Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 推送到Deque的函数/方法指针_C++_Function Pointers_Deque_Member Function Pointers - Fatal编程技术网

C++ 推送到Deque的函数/方法指针

C++ 推送到Deque的函数/方法指针,c++,function-pointers,deque,member-function-pointers,C++,Function Pointers,Deque,Member Function Pointers,我正在排队运行函数。我将需要调用的函数放入一个std::deque中,然后我随后循环调用每个函数并让其运行,有时甚至根据返回做一些事情 我遇到的问题实际上是关于将这些函数放在deque中 我在一个名为A2\u Game的类中有一个deque。我还有一个名为Button的类 我的代码如下所示: class Button { bool DoInput(); } class A2_Game { std::deque<bool(*)()> Input_Functions;

我正在排队运行函数。我将需要调用的函数放入一个
std::deque
中,然后我随后循环调用每个函数并让其运行,有时甚至根据返回做一些事情

我遇到的问题实际上是关于将这些函数放在deque中

我在一个名为
A2\u Game
的类中有一个deque。我还有一个名为
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
}
class按钮
{
booldoinput();
}
A2类游戏
{
std::deque输入函数;
bool EnterName()
}
A2_游戏::其他方法()
{
按钮*btn1=新按钮();
Input_Functions.push_back(&A2_Game::EnterName);//编译器告诉我这样做,但它仍然不会编译该行。。
输入函数。推回(btn1->DoInput);
//环路
}
我无法确定如何修复编译错误。我想你们中的一些人可能会通过看我在这里展示的内容,直接告诉我为了编译这个文件需要做哪些更改/工作。如果是这样的话!如果为true,则以下是编译错误

错误C2664:'std::deque::push_back':无法将参数1从'bool(uu thiscall A2_Game::*)(void)'转换为'bool(uu cdecl*const&)(void)


错误C3867:'Button::Doinput':函数调用缺少参数列表;使用“&Button::Doinput”创建指向成员的指针

如果您想推回函数,可以使用
std::function
(如果您的编译器不支持c++11,则使用boost)


如果您想推回函数,可以使用
std::function
(如果您的编译器不支持c++11,则可以使用boost)


问题是类方法的签名与函数签名
bool(*)(
)不匹配。这两种方法的签名是
bool(按钮::*)()
bool(A2_游戏::*)()分别为。(该方法所属的实际类是其签名的一部分!)

这里的解决方案是使用函子/函数对象。函子是围绕“可调用元素”的包装器对象,如果您想将函数视为对象(在OOP意义上),它们非常有用。如果您手头有类似的代码(代码编译):

#包括
#包括
类按钮
{
公众:
bool DoInput(){return true;}
};
A2类游戏
{
公众:
函数函子;
std::deque输入函数;
bool EnterName(){return true;}
void OtherMethod();
};
无效A2_游戏::其他方法()
{
按钮*btn1=新按钮();
输入函数。向后推(boost::bind(&A2_Game::EnterName,this));
输入函数。向后推(boost::bind(&Button::DoInput,btn1));
}
boost::bind
将函数指针和对实际类实例的引用组合在一起,并返回与
A2\u游戏::Functor
相同类型的函数对象


请注意,
boost::function
已集成到C++11标准中(请参阅),因此如果您的项目支持C++11,只需使用
#include
std
而不是
boost
名称空间。

问题是类方法的签名与函数签名
bool(*)不匹配
。这两种方法的签名是
bool(按钮::*)()
bool(A2_游戏::*)()分别为。(该方法所属的实际类是其签名的一部分!)

这里的解决方案是使用函子/函数对象。函子是围绕“可调用元素”的包装器对象,如果您想将函数视为对象(在OOP意义上),它们非常有用。如果您手头有类似的代码(代码编译):

#包括
#包括
类按钮
{
公众:
bool DoInput(){return true;}
};
A2类游戏
{
公众:
函数函子;
std::deque输入函数;
bool EnterName(){return true;}
void OtherMethod();
};
无效A2_游戏::其他方法()
{
按钮*btn1=新按钮();
输入函数。向后推(boost::bind(&A2_Game::EnterName,this));
输入函数。向后推(boost::bind(&Button::DoInput,btn1));
}
boost::bind
将函数指针和对实际类实例的引用组合在一起,并返回与
A2\u游戏::Functor
相同类型的函数对象


请注意,
boost::function
已集成到C++11标准中(请参阅),因此如果您的项目支持C++11,只需使用
#include
std
而不是
boost
名称空间。

您试图将成员函数指针分配给原始函数指针。那不行;)昆汀说的。编译器区分成员函数指针和原始函数指针。如果您想以同样的方式处理函数,我建议使用std::function和std::bind。您试图将成员函数指针分配给原始函数指针。那不行;)昆汀说的。编译器区分成员函数指针和原始函数指针。如果你想以同样的方式处理你的函数,我建议使用std::function和std::bind。哦,我真希望我现在就可以使用我的c++11编译器:(谢谢你的指导你可以使用boost::function和boost::bind而不是使用std::function和lambdasI也不能使用其他库:(哦,我真希望我现在就可以使用我的c++11编译器:(谢谢你的指导你可以使用boost::function和boost::bind而不是使用std::function和lambdasI也不能使用其他库:(
std::deque<std::function<bool()> > function_list;
Button* btn1 = new Button();
function_list.push_back([this](){return EnterName();});
function_list.push_back([btn1](){return btn1->DoInput();});
std::deque<boost::function<bool()> > function_list;
Button* btn1 = new Button();
function_list.push_back(boost::bind(&A2_Game::EnterName,this));
function_list.push_back(boost::bind(&Button::DoInput,btn1));
#include <boost/function.hpp>
#include <deque>

class Button
{
public:
    bool DoInput() { return true; }
};

class A2_Game
{
public:
    typedef boost::function<bool()> Functor;
    std::deque<Functor> Input_Functions;
    bool EnterName() { return true; }
    void OtherMethod();
};

void A2_Game::OtherMethod()
{
    Button* btn1 = new Button();
    Input_Functions.push_back(boost::bind(&A2_Game::EnterName, this));
    Input_Functions.push_back(boost::bind(&Button::DoInput, btn1));
}