【发布时间】:2014-06-03 12:12:02
【问题描述】:
我正在为运行函数创建一个队列。我将需要调用的函数放入 std::deque<bool(*)()> 中,然后我在 deque 中循环调用每个函数并让它运行,有时甚至根据返回来做事。
我遇到的问题实际上是关于将这些函数放在双端队列中。
我在一个名为A2_Game 的类中有这个双端队列。我还有一个叫Button的类。
我的代码如下所示:
class Button
{
bool DoInput();
}
class A2_Game
{
std::deque<bool(*)()> Input_Functions;
bool EnterName()
}
A2_Game::OtherMethod()
{
Button* btn1 = new Button();
Input_Functions.push_back(&A2_Game::EnterName); //The compiler told me to do this and it still won't compile the line..
Input_Functions.push_back(btn1->DoInput);
//Loop
}
我无法确定如何修复我的编译错误。我怀疑你们中的一些人可能能够通过查看我在这里展示的内容直接告诉我需要更改/完成什么才能编译它。如果是 !true 那么这里是编译错误。
error C2664: 'std::deque<_Ty>::push_back' : cannot convert parameter 1 from 'bool (__thiscall A2_Game::* )(void)' to 'bool (__cdecl *const &)(void)'
error C3867: 'Button::Doinput': function call missing argument list; use '&Button::Doinput' to create a pointer to member
【问题讨论】:
-
您正在尝试将 成员函数指针 分配给原始函数指针。那行不通;)
-
昆汀说了什么。编译器区分成员函数指针和原始函数指针。如果你想以同样的方式对待你的函数,我建议使用 std::function 和 std::bind。
标签: c++ function-pointers deque member-function-pointers