c++classlualuabind

Lua with Luabind compilation error c++


I can't get it to work.

Here is my code, I kept it as simple as possible :

#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);
}

The compiler gives me alot of garbage information, and by redirecting the output, i see first :

/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*)’

Line 48 is :

}

Can anyone help me please ? I'm sure it is a beginner mistake..


Solution

  • You may not declare attributes with .def since it's only used for functions. Depending on how you want to access your class members you can use different approaches:

    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;
    

    Check the doku for a detailed explanation: LuaBind Documentation