Meneliki wrote:
Map[,] = new Area[8, 8]; //8,8 would actually give you more than 64 elements because arrays start at 0, not 1.. you could try 7,7
What C++ book have you been reading?
The comma operator will not do what you think it does. Also, C++ uses exclusive upper-bounds, so 8,8 would be correct if only it actually worked :p
"Area *area" is by definition an array with a single index.
The reason is that C++ does not keep array sizes, so area[i][j] cannot be translated to area[i*Z + j] internally (where Z is the size of the 2nd index).
If you don't mind a fixed size world, the simplest solution is to declare
Quote:
Area area[8][8];
Here you make an array of arrays of Area objects, and "area[i][j]" will give you the right object.
In your "Area = new area[8][8]" you swapped the variable and the type.
If you want it dynamic, you can do something like
Quote:
Area *area;
int sz;
area = new Area[64];
sz = 8;
Area *Get(int i, int j) { return &area[i*sz + j]; }
"Get(3, 5)->height = 18;" would work then. This is also useful when you create parts of the world on demand, that is, the 'Get' could create new areas, or load them from disk or whatever.
Even more dynamic would be
Quote:
Area *rows[];
rows = (Area**)malloc(8*sizeof(Area *)); // The cast may fail, I have not actually tried it.
for (int i = 0; i < 8; i++) { rows[i] = new Area[8]; }
That is, make an array of pointers to an array of objects.
_________________
My project: Messing about in
FreeRCT,
dev blog, and IRC #freerct at oftc.net