Pointer to 2D array of objects (C++)
Posted: Thu Jun 28, 2007 11:41 pm
I'm writing a game with tile based map. The map can contain a dynamic number of tiles, each Tile being its own object (it contains info about what sort of tile it is along with a 3D mesh).
In my Tile Manager class (a class which handles all the map tiles) I have made a pointer to a 2D array of Tile objects:
How can I allocate this pointer and the tile objects?
I have thought of doing something like:
But, I get the following errors:
I'm going to use a linked list, but if any one knows how to create a pointer to a 2D array I would be greatfull.
A 2D array would be faster - I could look up a tile by using m_tile[x][y] rather than doing thousands of searches through a linked list (the map size could but up to 100x100 tiles).
In my Tile Manager class (a class which handles all the map tiles) I have made a pointer to a 2D array of Tile objects:
Code: Select all
Tile **m_tiles;
I have thought of doing something like:
Code: Select all
Tile tiles[m_width][m_height];
for(int x = 0; x < m_width; x++)
for(int y = 0; y < m_height; y++)
{
tile = new Tile(x,y,TileType::Grass);
}
m_tiles = &Tile;
Code: Select all
1>.\tilemanager.cpp(21) : error C2057: expected constant expression
1>.\tilemanager.cpp(21) : error C2466: cannot allocate an array of constant size 0
1>.\tilemanager.cpp(21) : error C2057: expected constant expression
1>.\tilemanager.cpp(21) : error C2466: cannot allocate an array of constant size 0
1>.\tilemanager.cpp(21) : error C2087: 'tiles' : missing subscript
1>.\tilemanager.cpp(21) : error C2133: 'tiles' : unknown size
1>.\tilemanager.cpp(21) : error C2512: 'Tile' : no appropriate default constructor available
A 2D array would be faster - I could look up a tile by using m_tile[x][y] rather than doing thousands of searches through a linked list (the map size could but up to 100x100 tiles).