【发布时间】:2017-03-27 18:12:29
【问题描述】:
我尝试在抽象类中将std::bind 与虚拟纯函数一起使用,但是
我使用设计模式调用策略,因为我想做一个可以处理游戏之间动态切换的程序。
我不明白语法。这是代码:
这是我的接口类
class IGame
{
public:
virtual ~IGame(){};
virtual void move_up(INFO &info)=0;
}
顺便说一下INFO 是一个定义:
#define INFO std::pair<struct GetMap*, struct WhereAmI*>
这是我调用std::bind调用的构造函数中的控件类;
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);
}
这是CGame类的构造函数:
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
}
所以第二个push_back 犯了这个错误:
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));
我该怎么办?
对不起,我糟糕的英语和 c++ 代码。
【问题讨论】:
-
请不要使用
#definetypedef会这样做 -
你的问题不是很清楚,因为编译器已经说你不能这样做。您应该改写问题以询问您实际想要实现的目标。就目前而言,它无法修复
标签: c++ function c++11 abstract-class stdbind