我在头文件中有简单的类:a.hh
#ifndef a_hh
#define a_hh
class a
{
public:
int i;
a()
{
i = 0;
}
};
#endif
然后我有一个文件:b.cc
#include <iostream>
#include "a.hh"
using namespace std;
int main(int argc, char** argv)
{
a obj;
obj.i = 10;
cout << obj.i << endl;
return 0;
}
>
到目前为止一切都很好。
我编译了代码,它编译得很好。
但是一旦我在类中添加一个向量:
#ifndef a_hh
#define a_hh
class a
{
public:
int i;
vector < int > x;
a()
{
i = 0;
}
};
#endif
我收到如下编译错误:
> CC b.cc
"a.hh", line 7: Error: A class template name was expected instead of vector.
1 Error(s) detected.
在这里将向量声明为成员有什么问题?
你需要#include <vector>
并使用限定名称std::vector<int> x;
:
#ifndef a_hh
#define a_hh
#include <vector>
class a{
public:
int i;
std::vector<int> x;
a() // or using initializer list: a() : i(0) {}
{
i=0;
}
};
#endif
其他要点:
- (如 EitanT 评论)我删除了附加资格
a::
在构造函数上
- 读过为什么“使用命名空间 std”被认为是不好的做法? https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)