简体   繁体   English

我该如何修复此 FSM C++ 语法错误 C3867

[英]how do i fix this FSM C++ syntax error C3867

so I'm new to c++ and trying to create a FSM for this RPG game but i get 1 syntax error: C3867 non-standard syntax; use '&' to create a pointer to member所以我是 c++ 的新手,并试图为这个 RPG 游戏创建一个 FSM,但我得到 1 个语法错误: C3867 non-standard syntax; use '&' to create a pointer to member C3867 non-standard syntax; use '&' to create a pointer to member but when i use the '&' i get another error C2276: '&': illegal operation on bound member function expression here's my codes: C3867 non-standard syntax; use '&' to create a pointer to member但是当我使用“&”时,我得到另一个error C2276: '&': illegal operation on bound member function expression这是我的代码:

Class CNPC
{
private : 
    State <CNPC>* CurrentState;
    State<CNPC>* PrevState;
public:
    void ChangeState(State <CNPC> * NewState)
    void revertToPrevState(); 
    void OnUpdate(Uint32 time, Uint32 deltaTime)
}

CNPC.cpp for the change state function CNPC.cpp 用于更改状态函数

void CNPC::changeState(State<CNPC>* pnewState)
{

    assert(CurrentState && pnewState);

    PrevState = CurrentState;

    CurrentState->Exit(this);

    CurrentState = pnewState;

    CurrentState->Enter(this);

}

the states header file状态头文件

class P_Attack : public State<CNPC>
{
public:
    static P_Attack* Instance();
    virtual void Enter(CNPC* npc);
    virtual void Exucute(CNPC* npc);
    virtual void Exit(CNPC* npc);
private:
    P_Attack() {};
};

cpp file文件

P_Attack* P_Attack::Instance() // the same layout in all of the states 
{
    static P_Attack* S;
    return S;
}

void P_Attack::Exucute(CNPC*npc)
{ 

    npc->Attacked();


    if(npc->GetHealth() <= 0)
    {
        npc->changeState(Die::Instance()->Enter); // this is where im getting the error
    }

im sure the answer is obvious but it continues to elude me, pretty sure im doing something very stupid我确定答案很明显,但它仍然在躲避我,很确定我在做一些非常愚蠢的事情

The error you are receiving is due to the fact that you are accidentally passing a bound member function reference as an argument to the changeState function.您收到的错误是由于您不小心将绑定成员函数引用作为参数传递给 changeState 函数。

When you pass Die::Instance()->Enter as an argument, you aren't passing the return of the Enter member function but rather attempting to pass the memory address of the Enter function itself.当您将Die::Instance()->Enter作为参数传递时,您并没有传递Enter成员函数的返回值,而是试图传递Enter函数本身的内存地址。 This explains the first error telling you to append & as the compiler believes you are actually wanting to pass a function reference.这解释了第一个错误告诉您附加&因为编译器认为您实际上想要传递函数引用。

The following issue is that the syntax for passing member functions by reference requires you to refer to their class name, followed by scope resolution :: followed by the member function name, hence getting an error if you try to pass &Die::Instance()->Enter as you are not properly passing a member function since member functions belong to the class, not to an instance.以下问题是通过引用传递成员函数的语法要求您引用它们的类名,然后是作用域解析:: ,然后是成员函数名称,因此如果您尝试传递&Die::Instance()->Enter会出错&Die::Instance()->Enter因为您没有正确传递成员函数,因为成员函数属于类,而不是实例。 So you receive an error saying the operation is illegal as you are attempting to pass a member function which is bound to an object as if it were unbound.因此,当您尝试传递一个绑定到对象的成员函数时,您会收到一条错误消息,指出该操作是非法的,就好像它是未绑定的一样。 Essentially what the compiler expects to see is &state<CBNC>::Enter because the Enter method is bound to your state class, but it sees you attempting to take the address of the Enter function as if it belonged to something else.本质上,编译器期望看到的是&state<CBNC>::Enter ,因为Enter方法绑定到您的状态类,但它看到您试图获取Enter函数的地址,就好像它属于其他东西一样。

That all said, assuming that Die::Instance()->Enter() requires no arguments, appending () as I just showed should resolve those errors, but not the underlying issue.综上所述,假设Die::Instance()->Enter()不需要参数,如我刚才所示附加()应该可以解决这些错误,但不能解决根本问题。 I say this because it appears that your Instance() member function is likely a factory which returns the state object you really want to pass.我这样说是因为看起来你的Instance()成员函数很可能是一个工厂,它返回你真正想要传递的状态对象。 If that is in fact the case simply passing Die::Instance() should suffice since the returned object will then invoke Enter once inside of your changeState function, properly invoking Enter on your instance as expected.如果实际上是这种情况,只需传递Die::Instance()就足够了,因为返回的对象随后将在您的 changeState 函数中调用一次Enter ,并按预期在您的实例上正确调用Enter I say it won't solve the underlying issue as it appears that your Enter() functions return void, thus simply changing the syntax to the above would attempt to pass the return of Die::Instance()->Enter() which would be void as the last function to execute is Enter() causing another error to appear.我说它不会解决根本问题,因为您的Enter()函数似乎返回 void,因此只需将语法更改为上面的语法就会尝试传递Die::Instance()->Enter()的返回值,这将无效,因为最后执行的函数是Enter() ,导致出现另一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM