mardi 28 juin 2016

Initializing a 2D vector in member initializer lists of variable size?

I'm looking to initialize a 2D vector in the member initialization list of a constructor, but I want the size of it to be variable so I can pass that in as a parameter.

If I have the 2D vector member below:

std::vector<std::vector<int> > myVectorMember;

And this is my constructor:

MyClass::MyClass(int x, int y)
: myVectorMember(x, std::vector<int>(y, 0)
{}

Then I get the error:

error: array initializer must be an initializer list
: myVectorMember(x, std::vector<int>(y, 0)
  ^

What is the correct way to do this if any?

Aucun commentaire:

Enregistrer un commentaire