UPDATE: Most of my relevant source code is in this pastebin: http://pastebin.com/nhAx1jfG
Can anyone make sense of this error? I’m trying to access a 2d “Tile” array I declare in a different class in the same namespace, it’s public, and I do declare an object in the main “rouge.cpp” class.
g++ -Wall -Werror -lncurses -c rouge.cpp -o rouge.o
rouge.cpp: In function ‘void update_game(Player, Map, World, bool)’:
rouge.cpp:497: error: no match for ‘operator[]’ in ‘*(world.World::tiles + ((((unsigned int)player.Player::<anonymous>.GameObject::x) + 0xffffffffffffffffffffffffffffffffu) * 16u))[player.Player::<anonymous>.GameObject::y]’
rouge.cpp:506: error: no match for ‘operator[]’ in ‘*(world.World::tiles + ((((unsigned int)player.Player::<anonymous>.GameObject::x) + 1u) * 16u))[player.Player::<anonymous>.GameObject::y]’
rouge.cpp:515: error: no match for ‘operator[]’ in ‘*(world.World::tiles + ((unsigned int)(((unsigned int)player.Player::<anonymous>.GameObject::x) * 16u)))[(player.Player::<anonymous>.GameObject::y + -0x00000000000000001)]’
rouge.cpp:524: error: no match for ‘operator[]’ in ‘*(world.World::tiles + ((unsigned int)(((unsigned int)player.Player::<anonymous>.GameObject::x) * 16u)))[(player.Player::<anonymous>.GameObject::y + 1)]’
rouge.cpp:540: error: no match for ‘operator[]’ in ‘*(world.World::tiles + ((unsigned int)(((unsigned int)player.Player::<anonymous>.GameObject::x) * 16u)))[player.Player::<anonymous>.GameObject::y]’
make: *** [rouge.o] Error 1
world.h:
// The "world" class, generates a world.
class World
{
// Public classes and variables
public:
// Tile array
Tile* tiles;
int plates[WORLDSIZEX][WORLDSIZEY];
//Tile tiles[WORLDSIZEX][WORLDSIZEY];
//Tile (*tiles)[WORLDSIZEX] = new Tile[WORLDSIZEX][WORLDSIZEY];
// For plates
//int plates[WORLDSIZEX][WORLDSIZEY];
//plates = new int[WORLDSIZEX][WORLDSIZEY];
//int (*plates)[WORLDSIZEX] = new int[WORLDSIZEX][WORLDSIZEY];
// Small world
Tile smalltiles[SMALLWORLDX][SMALLWORLDY];
// For world temp
int worldtemp;
// Constructor
World();
void generate_plates();
bool check_plates();
int build_mountains(int,int);
void add_mountains();
void generate_world();
void shrink_world();
void save_world();
void erupt();
bool get_passable(int,int);
char get_icon(int,int);
char get_small_icon(int,int);
int get_color(int,int);
int get_small_color(int,int);
};
world.cpp where I allocate the array:
// Constructor
World::World()
{
// Seed for random number
srand( time(NULL) );
tiles = new Tile[WORLDSIZEX][WORLDSIZEY];
// Generate a world
generate_world();
// Shrink world
shrink_world();
// Then save it.
//save_world();
}
in rouge.cpp, I access it just like:
world.tiles[i][j]; //i and j are ints in a nested for loop
after I declare it like:
World world;
and it’s spitting out that error
You’re using a pointer to point to an array that was dynamically allocated as a multidimensional array. After the address of this multidimensional array is stored in
World::tiles, the compiler “forgets” that the array was multidimensional. Thus, you can no longer use the double brackets notation (tiles[x][y]) to access an element.To get around this, you have at least 4 options:
Provide a method in World that takes x,y coordinates and returns a reference to the desired cell (you might want to provide both const and non-const versions):
Tile& at(size_t x, size_t y) {return tiles[y*WORLDSIZEX + x];}const Tile& at(size_t x, size_t y) const {return tiles[y*WORLDSIZEX + x];}Roll-up your own 2D array class for
World::tiles.Use a boost::multi_array for
World::tiles.Declare
World::tilesasvector< vector<Tile> >. The initial sizing of all nested vectors is tricky, but after that you can use theworld.tiles[x][y]notation.Plus the option @fsmc gave you.
I took a quick look at your code in the pastebin. It would be much more efficient (and cleaner looking) if you initialized your
tilesvectors like this instead: