【问题标题】:Exposing an STL Queue to Lua via Luabind通过 Luabind 向 Lua 公开 STL 队列
【发布时间】:2011-09-30 01:08:40
【问题描述】:

我正在尝试用 STL Queue 类替换用 Lua 编写的队列类的现有实现。我不确定为什么会失败,或者如何解决它。下面是一些显示相同行为的示例代码以及错误输出。提前致谢!

#include 
#include 

结构 XYZ_T
{
    短 x, y, z;
};

typedef std::queue XYZ_QUEUE_T;

extern "C" int init(lua_State *L)
{
    使用命名空间 luabind;

    打开(L);

    模组(L)
    [
        class_("XYZ_T")
            .def(构造函数())
            .def_readwrite("x", &XYZ_T::x)
            .def_readwrite("y", &XYZ_T::y)
            .def_readwrite("z", &XYZ_T::z),

        class_("XYZ_QUEUE_T")
            .def(构造函数())
            .def("推", &XYZ_QUEUE_T::推)
            .def("pop", &XYZ_QUEUE_T::pop)
            .def("前", &XYZ_QUEUE_T::front)
            .def("后退", &XYZ_QUEUE_T::back)
            .def("空", &XYZ_QUEUE_T::empty)
            .def("大小", &XYZ_QUEUE_T::size)
    ];
}


以及 gcc 输出:

g++ -o test_luabind.os -c -fPIC -Iinclude -I$VALID_INCLUDE_DIR /packages/build_env/include test_luabind.cpp test_luabind.cpp:在函数“int init(lua_State*)”中: test_luabind.cpp:27:错误:没有匹配的函数调用` luabind::class_::def(const char[6], )' test_luabind.cpp:32:错误:在 `(' 标记之前解析错误

【问题讨论】:

    标签: c++ stl lua queue luabind


    【解决方案1】:

    很可能,您的队列实现中有一个重载函数。因此,当您获取地址时,编译器不知道该怎么做,因为您可能指的是任何重载函数。

    【讨论】:

    • 如果这是真的,那就是std::queue::front,它有一个const 和非常量版本。 back 有类似的版本。
    • 太棒了!就是这样。现在看起来很明显:-)
    【解决方案2】:

    正如 DeadMG 指出的那样,在重载函数的情况下,您必须告诉编译器选择哪个版本,例如对于const 版本:

    typedef const XYZ_QUEUE_T::value_type& (XYZ_QUEUE_T::*ConstRefConstType)() const;
    
    class_<XYZ_QUEUE_T>("XYZ_QUEUE_T")
        // ...
        .def("front", (ConstRefConstType)&XYZ_QUEUE_T::front)
        .def("back" , (ConstRefConstType)&XYZ_QUEUE_T::back)
        // ...
    

    luabind 文档包括an example of this

    【讨论】:

      【解决方案3】:

      这个:

      class_("XYZ_QUEUE_T")
      

      应该是:

      class_<XYZ_QUEUE_T>("XYZ_QUEUE_T")
      

      字符串是 Lua 看到的类型名,而模板参数是你正在使用的 C++ 类型。

      哦,如果您不公开队列存储的对象,那么将队列公开给 Lua 并没有什么好处。

      【讨论】:

      • 呸,我有那个,但我忘了用 HTML 编码 <象征。此外,定义 XYZ_T 似乎并没有影响这个问题。长话短说,同样的问题:-(
      猜你喜欢
      • 2013-01-29
      • 1970-01-01
      • 2020-12-17
      • 2021-11-07
      • 2015-05-14
      • 2011-01-18
      • 2012-07-21
      • 2014-09-25
      相关资源
      最近更新 更多