尝试将函数绑定到lua时出现C ++错误

时间:2013-06-13 17:58:29

标签: c++ c 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);
    }
}

1 个答案:

答案 0 :(得分:0)

lua_register仅直接支持自由函数(或静态方法,除可见性外,它们相同)。因此,您必须找到另一种传递对象指针的方法。如果你只需要一个单例(即你想为脚本函数的所有调用共享相同的状态),最简单的方法是私有静态变量(实例指针)。

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