array error help!

I'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

The compiler has to create code that reserves memory for the local automatic variable currenttowns. The code has to know how many bytes from stack is used for that array when this function is called.

How many bytes? We do not know that during compilation. We know it when the program runs.

The C standard does support variable-length arrays (VLA) and VLA was proposed for C++14 standard, but apparently didn't quite make it. IMHO, the already existing dynamic allocation facilities in C++ are more than enough, although a dynamic pointer to array could be nice syntactic sugar.

The facilities ...
1
2
3
std::vector<std::string> currenttowns;
currenttowns.reserve( townlimit );
currenttowns.resize( townstartlimit );


PS. The compiler version and options used by the IDE are much more important than the IDE.
Topic archived. No new replies allowed.