I want to declare a vector of 2 elements as a class member. But next code generates an error:
class A {
private:
std::vector v (2);
...
}
You can safely use this syntax:
std::vector<int> v = std::vector<int>(2);
Alternatively, use brace initialization, but you must be careful: the std::initializer_list<int>
constructor will be picked, so to initialize a vector with two value- (therefore zero-) initialized ints
you need
std::vector<int> v{0, 0};
An in-class initialiser has to use braces or the equals sign; so this could be
std::vector<int> v = std::vector<int>(2);
or
std::vector<int> v {0,0}; // Careful! not {2}
Alternatively, you could use old-school initialisation in the constructor(s):
A() : v(2) {}