C++11 luabind 集成,函数失败。

我正在尝试使用luabind将LUA集成到我的程序中,但我遇到了一个大障碍。

我非常不熟悉LUA的调用约定,感觉我丢失了一些简单的东西。

这是我的C++代码:

struct app_t
{
    //...
    void exit();
    void reset();

    resource_mgr_t resources;
    //...
};

struct resource_mgr_t
{
    //...
    void prune();
    void purge();
    //...
};

extern app_t app;

以及我的luabind模块:

luabind::module(state)
    [
        luabind::class_<resource_mgr_t>("resource_mgr")
        .def("prune", &resource_mgr_t::prune)
        .def("purge", &resource_mgr_t::purge)
    ];

luabind::module(state)
    [
        luabind::class_<app_t>("app")
        .def("exit", &app_t::exit)
        .def("reset", &app_t::reset)
        .def_readonly("resources", &app_t::resources)
    ];

luabind::globals(state)["app"] = &app;

我可以很好地执行以下LUA命令:

app:exit()
app:reset()

但以下调用失败:

app.resources:purge()

并产生以下错误:

[string "app.resources:purge()"]:1: attempt to index field 'resources' (a function value)

非常感谢任何的帮助!

点赞
用户2226988
用户2226988

当绑定非原始类型成员时,自动生成的 getter 函数将返回对其的引用。

而且,就像 app:reset() 中一样,resources 是实例成员字段。

因此,使用方式如下:

app:resources():purge()
2014-10-09 08:44:02