Expression must have a constant value problem

cout << "Enter in width (X): ";
cin >> temp;
temp++;
const int maxX = temp;


cout << "Enter in height (Y): ";
cin >> temp;
temp++;
const int maxY = temp;

GridSpot map[maxX][maxY];

I don't get it. maxX and maxY ARE constants. Granted, they are being declared with the value of another variable (temp). And of course, when I change temp to an actual value (when declaring maxY and maxX), it works fine.
Arrays declared like that must have their dimension known at compile time.
Well, that's no fun :(
I never said you can't do what you want, you just have to write it slightly different.

1
2
3
4
5
GridSpot** map = new GridSpot*[maxX];
for(int i=0; i<maxX; ++i)
{
    map[i] = new GridSpot[maxY];
}


You could also just put both dimensions into a single x*y array.

Oh, and remember that you have to free that memory before the pointers go out of scope with delete.
Thanks!
I was afraid that I would have to change a bunch of my code. This is good news.
Topic archived. No new replies allowed.