【问题标题】:Lua with Luabind compilation error c++Lua与Luabind编译错误c ++
【发布时间】:2014-07-16 04:32:45
【问题描述】:

我无法让它工作。

这是我的代码,我尽量保持简单:

#include <iostream>
#include <lua/lua.hpp>
#include <luabind/luabind.hpp>
#include <luabind/class.hpp>

class           MyClass
{
public:
  MyClass() {}
  ~MyClass() {}
  void          myFunc() { std::cout << "Hi !" << std::endl; }
  int           getMyVar2() { return (this->myVar2); }
  int           myVar;
private:
  int           myVar2;
};

int             main()
{
  MyClass       *m = new MyClass();
  lua_State     *L = luaL_newstate();

  luabind::open(L);
  luabind::module(L)
    [
     luabind::class_<MyClass>("MyClass")
     .def("myFunc", &MyClass::myFunc)
     .def("getMyVar2", &MyClass::getMyVar2)
     .def("myVar", &MyClass::myVar)
     ];
  luabind::globals(L)["globalName"] = m;

  if (luaL_dofile(L, "example.lua"))
    fprintf(stderr, "%s\n", lua_tostring(L, -1));
  lua_close(L);
}

编译器给了我很多垃圾信息,通过重定向输出,我首先看到了:

/usr/local/include/luabind/class.hpp: In instantiation of ‘void luabind::detail::memfun_registration<Class, F, Policies>::register_(lua_State*) const [with Class = MyClass; F = int MyClass::*; Policies = luabind::detail::null_type; lua_State = lua_State]’:
exemple_luabind.cpp:48:1:   required from here
/usr/local/include/luabind/class.hpp:311:52: error: no matching function for call to ‘deduce_signature(int MyClass::* const&, MyClass*)’

第 48 行是:

}

谁能帮帮我?我确定这是初学者的错误..

【问题讨论】:

    标签: c++ class lua luabind


    【解决方案1】:

    你不能用 .def 声明属性,因为它只用于函数。 根据您想要访问班级成员的方式,您可以使用不同的方法:

    luabind::module(L)
            [
                luabind::class_<MyClass>("MyClass")
                .def("myFunc", &MyClass::myFunc)
                .def("getMyVar2", &MyClass::getMyVar2) // function with MyClass:getMyVar2()
                .property("myVar2", &MyClass::getMyVar2) // MyClass.myVar2 is only readable, same as MyClass:getMyVar2()
                .property("myVar", &MyClass::myVar) // MyClass.myVar is only readable
                // vs
                .def_readwrite("myVarT", &MyClass::myVar) // MyClass.myVarT is read and writable
            ];
        luabind::globals(L)["globalName"] = m;
    

    查看doku获取详细解释:LuaBind Documentation

    【讨论】:

    • 您好,谢谢!我使用 .def 作为变量,这给了我一个编译错误。我不知道那个.property。再次感谢!
    猜你喜欢
    • 2012-10-14
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 2016-12-07
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    相关资源
    最近更新 更多