Search code examples
c++lualuabridge

Error with "addProperty" in Luabridge


I have some very simple source code to expose a simple Foo class.

main.cpp:

#include <iostream>

#include <lua.hpp>
#include <LuaBridge.h>

class Foo
{
    private:
        int number = 0;

    public:
        void setNumber(const int& newNumber) {number = newNumber;}
        int getNumber() {return number;}
};

int main()
{
    //Expose the API:
    lua_State* L = luaL_newstate();
    luaL_openlibs(L);
    luabridge::getGlobalNamespace(L)
    .beginClass<Foo>("Foo")
        .addConstructor<void(*)(void)>()
        .addProperty("number", &Foo::getNumber, &Foo::setNumber)
    .endClass();
}

Unfortunately, I get this error:

24 error: no matching function for call to ‘luabridge::Namespace::Class<Foo>::addProperty(const char [7], int (Foo::*)(), void (Foo::*)(const int&))’

I don't know what the problem is, but I have to use addProperty otherwise the code doesn't look correct


Solution

  • The template for addProperty:

    template <class TG, class TS>
    Class <T>& addProperty (char const* name, TG (T::* get) () const, void (T::* set) (TS))
    

    requires that the getter is a const member function. Changing the getter to:

    int getNumber() const { return number; }
    

    removes the error in LuaBridge 2.0