我正在尝试在 *lua_CFunction* 上创建一个简单的 c++ 包装器,它的定义如下:
// header
typedef int (*lua_CFunction) (lua_State* lua);
...
lua_CFunction wrap (std::function <int (Game* game)> function);
// implementation
lua_CFunction ScriptingInterface::wrap (std::function <int (Game* game)> function)
{
return [this, function] (lua_State* unused) -> int {
int n_args = function (this->game);
return n_args;
};
}
void ScriptingInterface::registerFunction (std::string name, std::function <int (Game* game)> function)
{
lua_register (lua, name.c_str (), wrap (function));
}
这个想法是创建这样的公共功能:
int setTitle (Game* interface)
{
const char* title = lua_tostring (interface->getScripts ()->getLuaState (), 1);
SDL_WM_SetCaption (title, NULL);
return 0;
}
并与 lua 分享它们,例如:
scripts->registerFunction ("setTitle", setTitle);
scripts是ScriptingInterface的一个实例
尝试编译游戏时会出现此问题。
./scripting/scripting_interface.cc: In member function ‘int (* ScriptingInterface::wrap(std::function<int(Game*)>))(lua_State*)’:
./scripting/scripting_interface.cc:40:2: error: cannot convert ‘ScriptingInterface::wrap(std::function<int(Game*)>)::<lambda(lua_State*)>’ to ‘int (*)(lua_State*)’ in return
./scripting/scripting_interface.cc:41:1: warning: control reaches end of non-void function [-Wreturn-type]
谁能告诉我我在这里做错了什么,因为AFAIK代码应该可以毫无问题地编译?