C++11乱绑定集成,函数失败

C++11 luabind integration, function failing

本文关键字:函数 失败 集成 绑定 C++11      更新时间:2023-10-16

我正在尝试用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)

非常感谢您的帮助!

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

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

所以,像这样使用它:

app:resources():purge()