繁体   English   中英

尝试将函数绑定到lua的C ++错误

[英]C++ error trying to bind function to lua

我想我找到了答案,很快就会有结果

我收到此错误,不知道如何在不使方法为静态的情况下进行修复。 如果它们是静态的,我不会收到错误消息,但是我将无法使用我的任何非静态变量或函数。 同样,在您说出int和lua_CFunction是不同的类型之前,它们不是。 这是lua_CFunction的定义方式

typedef int (*lua_CFunction) (lua_State *L);

*

1   IntelliSense: argument of type "int (LuckyIrc::*)(lua_State *l)" is incompatible with parameter of type "lua_CFunction" f:\Programming\Visual Studio\C++\IrcBot\IrcBot\LuckyIrc.h   129 4   IrcBot

码:

class MyClass{
//other stuff
private:
//other stuff
    int Lua_SendMessage(lua_State *l);
    int Lua_SendRaw(lua_State *l);
    int Lua_SendAction(lua_State *l);
    int Lua_SendNotice(lua_State *l);
    int Lua_Quit(lua_State *l);
    int Lua_Part(lua_State *l);
    int Lua_SendNick(lua_State *l);
    int Lua_Kick(lua_State *l);
    int Lua_Join(lua_State *l);
    int Lua_Connect(lua_State *l);

    void SetUpLua()
    {
        LuaState = luaL_newstate();
        luaL_openlibs(LuaState);
        /*Error happens here*/lua_register(LuaState, "SendMessage", Lua_SendMessage);
        /*And here*/lua_register(LuaState, "SendRaw", Lua_SendRaw);
        /*And here*/lua_register(LuaState, "Quit", Lua_Quit);
        /*And here*/llua_register(LuaState, "Part", Lua_Part);
        /*And here*/lua_register(LuaState, "SendNotice", Lua_SendNotice);
        /*And here*/lua_register(LuaState, "SendAction", Lua_SendAction);
        /*And here*/llua_register(LuaState, "SetNick", Lua_SendNick);
        /*And here*/llua_register(LuaState, "Join", Lua_Join);
        /*And here*/llua_register(LuaState, "Kick", Lua_Kick);
        /*And here*/llua_register(LuaState, "Connect", Lua_Connect);
    }
}

lua_register仅直接支持自由函数(或静态方法,除了可见性外,其他方法相同)。 因此,您将不得不寻找另一种方法来传递对象指针。 如果只需要一个单例(即,您希望为脚本函数的所有调用共享相同的状态),则最简单的方法是私有静态变量(实例指针)。

另外,您可以尝试使用luabind库来更轻松地绑定自定义函数和类。

暂无
暂无

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

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