2D Vector won't compile

Hello everyone,

I'm having some difficulty with initializing my Vector. I'm trying to simulate a 2d array with vectors. The source looks good to me, but for some reason I'm getting an error at compile time.

In my Main.h file, I have the line:
std::vector< std::vector<char> > terrain;
which compiles ok.

In my Main.cpp file, I have the line:
this->terrain( Main::MAP_HEIGHT, std::vector<char>(Main::MAP_WIDTH, 'T') );
which ends up throwing this error:
../Main.cpp:93: error: no match for call to ‘(std::vector<std::vector<char, std::allocator<char> >, std::allocator<std::vector<char, std::allocator<char> > > >) (const int&, std::vector<char, std::allocator<char> >)’


MAP_HEIGHT and MAP_WIDTH are defined in my .h file as 10 and 20, respectively.

Does anyone have any idea why this error is occuring?

Thanks for any suggestions!

Chisser98
You are ... sort of ... attempting to call the vector<> constructor with your line of code
since you haven't actually specified the function to call.

First, I assume that the line of code is inside some class instance since you are using
this->.

What you want is vector<>::assign:

 
terrain.assign( Main::MAP_HEIGHT, std::vector<char>( Main::MAP_WIDTH, 'T' ) );

Hi jsmith,

Thank you for your reply, the vector<>::assign method compiled perfectly!

And yes, I was attempting to call the vector<> constructor. I saw an example that did this:

vector< vector<int> > vI2Matrix(3, vector<int>(2,0));

to create a '2d array' with vectors. So I split mine up and put the declaration of the variable in Main.h and the initialization in Main.cpp. Out of curiosity, any idea why what I was doing didn't work?

Thanks again for the reply!

Cheers
Variables can only be constructed at the point of instantiation.
This instantiates a variable named v and runs its constructor:

 
std::vector<char> v( 5, 'X' );  // v is now { 'X', 'X', 'X', 'X', 'X' } 


In your code, "terrain" was already instantiated elsewhere. Consequently,
the compiler was thinking that you wanted to call std::vector<...>::operator()
with the parameters Main::MAP_HEIGHT and std::vector<char>(Main::MAP_WIDTH, 'T').
But std::vector<> does not provide the function call operator.

I'm not sure where exactly you put that line of code -- be it in a class constructor or
elsewhere. For example, it is a common beginner (not saying you are one--I don't
know) error to do something like this:

1
2
3
4
5
6
7
8
9
10
class MyClass {
   std::vector<char> v;
  public:
   MyClass();
};

MyClass::MyClass()
{
   v( 5, 'X' );  // Wrong
}


The right way would be to put that line of code in an initializer list:

1
2
3
4
MyClass::MyClass() :
   v( 5, 'X' )  // Right!
{
}


Or, a bad, but workable solution would be:

1
2
3
4
MyClass::MyClass()
{
   v = std::vector<char>( 5, 'X' );  // OK, but the previous solution is preferred
}

Ahh okay, that makes perfect sense then. Thanks jsmith!

I'm experienced in Java and just learning the ropes of c++. Thanks again for the help!
Topic archived. No new replies allowed.