-
vector as private data
hey,
i'm just writing a small class now and i want a few vectors as private data. if i use the line
Code:
std::vector<int> a;
then there's no problem. But i want to declare that this vector should contain say, 32 elements, and that they are all initialise3d to 0, so i tried
Code:
std::vector<int> a(32, 0);
but it won't let me and errors with some
Code:
error C2059: syntax error : 'constant'
How can i specify the size and initial contents of the vector?
I don't get any errors if i use code like this in an ordinary program. Is there something special happening here cause it's in the private data section of a class?
Any ideas would be great.
-
just try specifying in the constructor
run a loop in the constructor that does it for ya
-
You can't call constructors directly in the class declaration, you must do it in the constructor chain of the class constructor.
Code:
class mine {
std::vector<int> vec;
public:
mine() : vec(32) {}
};