开发者

Exposing an STL Queue to Lua via Luabind

I'm attempting to replace an existing implementation of a queue class written in Lua with the STL Queue class. I'm not sure why this is failing, or how to approach fixing it. Below is some sample code that exhibits the same behavior, along with the error output. Thanks in advance!

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

struct XYZ_T
{
    short x, y, z;
};

typedef std::queue<XYZ_T> XYZ_QUEUE_T;

extern "C" int init(lua_State *L)
{
    using namespace luabind;

    open(L);

    module(L)
    [
        class_<XZY_T>("XYZ_T")
            .def(constructor<>())
            .def_readwrite("x", &XYZ_T::x)
            .def_readwrite("y", &XYZ_T::y)
            .def_readwrite("z", &XYZ_T::z),

        class_<XYZ_QUEUE_T>("XYZ_QUEUE_T")
            .def(constructor<>())
            .def("push",  &XYZ_QUEUE_T::push)
            .def("pop",   &XYZ_QUEUE_T::pop)
            .def("front", &XYZ_QUEUE_T::front)
            .def("back",  &XYZ_QUEUE_T::back)
            .def("empty", &XYZ_QUEUE_T::empty)
            .def("size",  &XYZ_QUEUE_T::size)
    ];
}


开发者_JAVA百科

And the gcc output:

g++ -o test_luabind.os -c -fPIC -Iinclude -I$VALID_INCLUDE_DIR /packages/build_env/include test_luabind.cpp
test_luabind.cpp: In function `int init(lua_State*)':
test_luabind.cpp:27: error: no matching function for call to `
   luabind::class_<XYZ_QUEUE_T, luabind::detail::unspecified, 
   luabind::detail::unspecified, luabind::detail::unspecified>::def(const 
   char[6], <unknown type>)'
test_luabind.cpp:32: error: parse error before `(' token


Most likely, you have an overloaded function in your queue implementation. Thus, when you take the address, the compiler doesn't know what to do because you could mean any overloaded function.


As DeadMG points out, in the case of overloaded functions you have to tell the compiler which version to pick, e.g. for the const versions:

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)
    // ...

The luabind documentation includes an example of this.


This:

class_("XYZ_QUEUE_T")

Should be:

class_<XYZ_QUEUE_T>("XYZ_QUEUE_T")

The string is the typename that Lua sees, while the template parameter is the C++ type you're using.

Oh, and it does little good to expose the queue to Lua if you don't expose the objects that the queue stores.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜