I have simple class in a header file: a.hh
#ifndef a_hh
#define a_hh
class a
{
public:
int i;
a()
{
i = 0;
}
};
#endif
Then i have a file: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;
}
>
Till this point everything is fine. I compile the code and it compiles fine. But as soon as i add a vector in the class:
#ifndef a_hh
#define a_hh
class a
{
public:
int i;
vector < int > x;
a()
{
i = 0;
}
};
#endif
I get a compilation error as below:
> CC b.cc
"a.hh", line 7: Error: A class template name was expected instead of vector.
1 Error(s) detected.
What is the problem with declaring a vector here as a member?
1.You need to
#include <vector>
andusing namespace std
, then a.hh just like below:2. If you don't want to only use std namespace in all your code, you can specified the namespace before type, just like
std::vector<int> x;
declaring a vector as a class member:
You need to
#include <vector>
and use the qualified namestd::vector<int> x;
:Other points:
a::
on the constructor