Вопрос

I'm having a very subtle problem with luabind v0.9.1 with g++ 4.7 (--std=c++11) and Boost 1.51 which can be reproduced in the following code:

#include <exception>
#include <iostream>
#include <string>

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

struct TestObject
{
    int x = 0;

    int increment()
    {
        return ++x;
    }
};

static void luaError(lua_State* stack, const std::string& luaCode, int luaErr)
{
    std::cerr << "Failed with code " << luaErr << ": " << std::endl
              << "LuaCode: " << luaCode << std::endl
              << "Message: " << lua_tostring(stack, -1) << std::endl;
    std::terminate();
}

void luaExec(lua_State* stack, const std::string& luaCode)
{
    if (int excode = luaL_loadbuffer(stack, luaCode.c_str(), luaCode.size(), "luaExec"))
        luaError(stack, luaCode, excode);

    if (int excode = lua_pcall(stack, 0, 0, 0))
        luaError(stack, luaCode, excode);
}

int main()
{
    using namespace luabind;
    lua_State* L = luaL_newstate();
    open(L);

    module(L)
    [
        class_<TestObject>("TestObject")
            .def(constructor<>())
            .def("increment", &TestObject::increment)
            .property("x", &TestObject::x)
    ];

    luaExec(L, "obj = TestObject()");
    luaExec(L, "obj.increment()");
}

Compiling and executing this code results in:

Failed with code 2: 
LuaCode: obj.increment()
Message: No matching overload found, candidates:
int increment(TestObject&)

However, if I change my second call to luaExec to luaExec(L, "obj.increment(obj)");, the code executes just fine. This behavior is quite odd. What am I doing wrong here?

Это было полезно?

Решение

Invoking methods on objects bound with luabind require invocation with object:method(arg1, arg2,...) syntax. This is semantic sugar for object.method(object, arg1, arg2, ....). This calls the correct method (object's), and also passes in the correct data member's ( object's). See http://lua-users.org/wiki/SimpleLuaClasses for a little more information on building classes directly in lua: this informs classes bound with luabind.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top