std :: function的初始化不会计算为带有3个参数的函数

时间:2015-03-01 17:58:47

标签: c++ c++11

GameObject类.h + .cpp:

typedef std::function<void(GameObject* triggerobject, GameObject* otherobject, TriggerAction action)> PhysicsCallback;

    void GameObject::OnTrigger(GameObject* triggerobject, GameObject* otherobject, TriggerAction action)
    {
        if (m_OnTriggerCallback)
            m_OnTriggerCallback(triggerobject, otherobject, action);
    }

    void GameObject::SetOnTriggerCallBack(PhysicsCallback callback)
    {
        m_OnTriggerCallback = callback;
    }

其他课程:

m_pSphere->SetOnTriggerCallBack(*pCbObj);
m_pSphere->OnTrigger(m_pWallLeft, m_pSphere, GameObject::TriggerAction(0));

我想要使用OnTrigger()函数来设置m_OnTriggerCallback。当我试图传递相应的论点时,我真的陷入困境。在没有编译器错误的情况下,似乎几乎不可能将PhysicsCallback初始化。

我试过了: std::function<void(GameObject* triggerobject, GameObject* otherobject, GameObject::TriggerAction action)> *obj; *obj = (m_pWallRight, m_pSphere, GameObject::TriggerAction(0));

但没有运气。 * obj不接受任何论据。以下这些行给出了相同的错误:

GameObject::PhysicsCallback *pCbObj; = new GameObject::PhysicsCallback(new std::function<void()>()); //term does not evaluate to a function taking 3 arguments
    GameObject::PhysicsCallback *pCbObj = new GameObject::PhysicsCallback((m_pWallRight, m_pSphere, GameObject::TriggerAction(0)));

这一行*pCbObj = GameObject::PhysicsCallback(m_pWallRight, m_pSphere, GameObject::TriggerAction(0));给出了这个智能感知错误: http://puu.sh/gi29n/95f0f7855b.png

我真的很困惑,如何使用SetOnTriggerCallBack函数?

2 个答案:

答案 0 :(得分:0)

m_pSphere->OnTrigger(m_pWallLeft, m_pSphere, GameObject::TriggerAction(0));
实际上,

GameObject::TriggerAction为参数调用0。然后,它传递结果。

什么是GameObject::TriggerAction?这是静态方法吗?这应该是因为,否则,您要std::bindobject instance或直接应用它。

答案 1 :(得分:0)

使用lambda,我能够创建一个编译器可接受的参数:

GameObject::PhysicsCallback trigger = [=](GameObject* triggerobject, GameObject* otherobject, GameObject::TriggerAction action){};