繁体   English   中英

Lua与Luabind编译错误C ++

[英]Lua with Luabind compilation error c++

我无法正常工作。

这是我的代码,我将其保持尽可能简单:

#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行是:

}

有人可以帮我吗? 我确定这是一个初学者的错误。

您不能使用.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文档

暂无
暂无

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

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