0

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

Ben Hollier
  • 585
  • 2
  • 11
  • 32

1 Answers1

1

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

ryanpattison
  • 6,151
  • 1
  • 21
  • 28
  • Making the getter to const doesn't fix it, It doesn't even change the error message – Ben Hollier Jul 24 '15 at 17:52
  • It both creates and removes the error for me with Lua 5.2 and LuaBridge 2.0 – ryanpattison Jul 24 '15 at 17:58
  • @Orfby which version of LuaBridge do you have? – ryanpattison Jul 24 '15 at 18:18
  • The latest version (from GitHub). I'll try reinstalling – Ben Hollier Jul 24 '15 at 18:38
  • Just reinstalled (LuaBridge 2.0), had to make some changes otherwise it doesn't compile (from [here](https://eliasdaler.wordpress.com/2014/07/18/using-lua-with-cpp-luabridge/)), still doesn't fix it. What version of LuaBridge do you have and do you get the errors after getting a clean copy? – Ben Hollier Jul 24 '15 at 18:44
  • I am using [LuaBridge 2.0](https://github.com/vinniefalco/LuaBridge) and compiled the code you have posted using `g++`. I had to add `--std=c++11 -llua5.2` and I received the same error. I updated with `const` and now a clean compile. Could you post the entire error message and retry (remember to save ;) ). – ryanpattison Jul 24 '15 at 19:01
  • What do you mean "entire error message"? The only part of the error message I cut off was the directory to where I was building, and I removed a couple of `|`. You also didn't say whether you can reinstall LuaBridge from GitHub and get it to work immediately (without changing code) – Ben Hollier Jul 24 '15 at 19:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84217/discussion-between-rpattiso-and-orfby). – ryanpattison Jul 24 '15 at 19:15