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?

1

1 Answer 1

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]
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.