lundi 27 mars 2017

Cannot bind function from abstract class in c++

I'm try to use std::bind with a virtual pure function in a abstract class but I use a design pattern call strategy, because I want make a program that can handle dynamic switch between the game.

I don't get the syntax. here is the code :

Here is my interface class

class IGame
{
  public:
    virtual ~IGame(){};
    virtual void move_up(INFO &info)=0;
}

By the way INFO is a define :

 #define INFO std::pair<struct GetMap*, struct WhereAmI*>

Here is my control class in it's constructor I call std::bind call;

 class CGame
  {
  private:
    IGame                                       *game;
    int                                         score;
    std::pair<struct GetMap*, struct WhereAmI*> info; // game information

    std::vector<std::function<void(std::pair<struct GetMap*, struct WhereAmI*>&)>> ptr_move_ft; //function pointer vector

  public:
    CGame();
    ~CGame();
    void return_get_map(INFO &info);
  }

This is the constructor of CGame class :

CGame::CGame()
 {
   game = new Snake();
   this->info = std::make_pair(init_map(MAP_PATH_SNAKE,0), game->init_player());

   ptr_move_ft.push_back(std::bind(&CGame::return_where_i_am, this,std::placeholders::_1)); //this work

   ptr_move_ft.push_back(std::bind(&game->move_up, game, std::placeholders::_1)); //this create a error

 }

So the second push_back makes this error :

source/CGame.cpp: In constructor ‘arcade::CGame::CGame()’:
source/CGame.cpp:131:44: error: ISO C++ forbids taking the address of a bound member function to form a pointer to member function.  Say ‘&arcade::IGame::move_up’ [-fpermissive]
     ptr_move_ft.push_back(std::bind(&game->move_up, game, std::placeholders::_1));

How can I do ?

Sorry for my poor English and c++ code.

Aucun commentaire:

Enregistrer un commentaire