vendredi 6 mai 2016

Insert std::unique_ptr into a nested map

I am trying to make a game with a map of fields. My nested map looks like this

typedef std::map<unsigned int, std::map<unsigned int, std::unique_ptr<Field> Board;

Board is a member of my Game class which contains a lot of the logic. Basically I am trying to do this:

Board board_;
unsigned int x = 0;
unsigned int y = 0;
board_[x][y] = std::unique_ptr<Field>(new Wall(x, y));

But I get this error:

Game.cpp: In member function ‘int Game::addTileToBoard(unsigned int, unsigned int, char)’:
Game.cpp:103:51: error: expected primary-expression before ‘(’ token
 board_[x][y] = std::move(std::unique_ptr<Tile>(new Wall(x, y)));

OR:

Game.cpp:103:41: error: expected primary-expression before ‘(’ token
 board_[x][y] = std::unique_ptr<Field>(new Wall(x, y));

So how do I create an object and add a unique_ptr to it to a nested map? Also I am bound to use c++11 so make_unique is not an option :( any ideas? Ideally I do not want to use map::insert since this method should also be able to simply override existing values. Using the asignment operator should just do that but for some reason it just won't compile..

Thanks!

Aucun commentaire:

Enregistrer un commentaire