2

So I have a private member in the class Map:

char **_map;

I then try to initialize the pointer array to a two dimensional char array like this:

std::vector<std::string> contents = StringUtils::split(_mapInfo.getContents(), ' ');
const int x = StringUtils::toInt(contents.at(0));
const int y = StringUtils::toInt(contents.at(1));
_map = new char[x][y];

Basically the contents vector contains two strings, which I then convert into integers. I then try to initialize the map array but I receive this error:

Error   1   error C2540: non-constant expression as array bound 

And this:

Error   2   error C2440: '=' : cannot convert from 'char (*)[1]' to 'char **'   

And finally this:

    3   IntelliSense: expression must have a constant value 

The last error references the variable y

Can anyone explain what is happening and how I can fix it?

user3316633
  • 137
  • 5
  • 13
  • 3
    This question has been answered in [How do I declare a 2d array in C++ using new?](http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new) – MicroVirus Jul 04 '14 at 15:26

1 Answers1

2

The initialization of 2d array is as following;

char **_map;

_map = new char*[rowsize];

for(int row = 0; row < rowsize; ++row)
{
    _map[row] = new char[columnsize]
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Eduard Rostomyan
  • 7,050
  • 2
  • 37
  • 76