game array help!

'm making a game, and currently i'm making a class to help with creating the map for the game, but when I compile I get an error


invalid use of non-static data member ‘gameMap::townstartlimit’ on line: 2

and on line 3 it says: from this location


and my code is:

1
2
3
int townlimit=rand() % 20 + 1; //max amount of towns in a game
int townstartlimit=round((townlimit/2));//the amount of natural towns to spawn when map is created 
string currenttowns[townstartlimit];



ps.: I'm using Code::Blocks 13.12
You can only initialize non-static data member variables like that in C++11. Maybe your compiler is too old or maybe you just need to enable C++11 somewhere in the settings. The traditional way to initialize is to do it in the constructor.
The compiler doesn't know what townstartlimit will be, so you have to either use dynamic memory allocation, or set the array limit arbitrarily high like this:

string currenttowns[16];


Topic archived. No new replies allowed.