鱼C论坛

 找回密码
 立即注册
查看: 1492|回复: 3

[已解决]容器vector不能放在类里面吗?

[复制链接]
发表于 2018-9-27 22:11:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 20180906 于 2018-9-27 22:11 编辑

为什么把vector放在类里面就不能用了?

主程序test.cpp 代码如下:
#include <iostream>
#include "Virtual.h"
#include "virtualImpl.h"
int main()
{
        Virtual * v = new VirtualImpl();
        v->fun();
        return 0;
}
父类Virtual.h 代码如下:
#ifndef VIRTUAL_H
#define VIRTUAL_H
#include <vector>
class Virtual
{
private:
        int a;
        vector<int> aa;//加了这一句之后代码编译不通过,不加之前可以运行
public:
        virtual void fun()=0;
};

#endif
子类VirtualImpl.h 代码如下:
#ifndef VIRTUALIMPL_H
#define VIRTUALIMPL_H
#include <iostream>
#include "virtual.h"
using namespace std;


class VirtualImpl:public Virtual
{
public:
        void fun(){cout << a<< endl;cout << "123" << endl;}
};

#endif
这就是整个项目了。
然后编译不过的 报错信息如下:

--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
f:\microsoft visual studio\myprojects\test\virtual.h(8) : error C2143: syntax error : missing ';' before '<'
f:\microsoft visual studio\myprojects\test\virtual.h(8) : error C2501: 'vector' : missing storage-class or type specifiers
f:\microsoft visual studio\myprojects\test\virtual.h(8) : error C2059: syntax error : '<'
f:\microsoft visual studio\myprojects\test\virtual.h(8) : error C2238: unexpected token(s) preceding ';'
执行 cl.exe 时出错.

test.exe - 1 error(s), 0 warning(s)

求大佬帮帮看看,告诉我这是什么情况?怎么解决呀??
最佳答案
2018-9-30 14:16:02
using namespace std?? std::vector
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-9-27 22:50:06 | 显示全部楼层
std::vector<int> aa;

vector在std空间
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-30 14:16:02 | 显示全部楼层    本楼为最佳答案   
using namespace std?? std::vector
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-1 23:14:40 From FishC Mobile | 显示全部楼层
这样呀,我先试试哈
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-2 16:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表