CompilingCyborg
CompilingCyborg

Reputation: 4860

Initializing and Destructing a 2D Vector in C++

Please I have two questions in respect to vectors in C++:

  1. How to fix the problem in the following code:

    • In my header file I have:

      vector< vector< char > > vec;
      
    • In my (.cpp) file in the definition of the constructor I have:

      vec(20, vector<char>(25, " "));
      
    • The error I'm receiving is the following:

      error: invalid conversion from 'const char*' to 'char'
      

    I know there is something wrong but I have no idea how to fix it.

  2. After the end of my program, how do I properly destruct my two dimensional vector in order to free the memory?

Any suggestions, ideas or solutions to my questions are greatly appreciated.

Upvotes: 2

Views: 1096

Answers (1)

Mysticial
Mysticial

Reputation: 471359

You probably wanted single quotes ' ':

vec(20, vector(25, ' '));

Otherwise, you're be passing a string " " which is causing that error.

As for your second question. You don't need to destroy it. It will automatically free itself when it falls out of scope.

EDIT:

You also need to do it together:

vector< vector< char > > vec(20, vector<char>(25, ' '));

You can't separate the declaration and the initializer like that. (at least not without an extra assignment.)

Upvotes: 2

Related Questions