首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用LuaBridge导出容器类

使用LuaBridge导出容器类
EN

Stack Overflow用户
提问于 2020-11-06 16:14:38
回答 2查看 97关注 0票数 0

我想将SRect和SRectVector从C++导出到Lua,但编译失败。正确的做法是什么?编译器: vs2019,vc++11 OS: Win10 64

Push()遇到编译错误,

我认为参数只是SRectVector *,为什么编译器认为它是'std::vector>'?

代码语言:javascript
复制
    class SRect{
    public:
        int left;
        int top;
        int right;
        int     bottom;
        SRect(int l, int t, int r, int b)
            : left(l)
            , top(t)
            , right(r)
           , bottom(b){}
        //...
    };
    
    typedef std::vector<SRect>    SRectVector;
    
    luabridge::getGlobalNamespace(L)
        .beginClass <SRect>("SRect")
            .addConstructor <void(*) (int, int, int, int)>()
            .addProperty("left", &SRect::left)
            //...
        .endClass()
        .beginClass <SRectVector>("SRectVector")
            .addFunction("Push",
                std::function <void(SRectVector*, const SRect&)>(
                    [](SRectVector* vec, const SRect& rc) { (*vec).push_back(rc); }))
            //...
        .endClass()
    .endNamespace();
    
    ```
    
1>E:\Code\include\LuaBridge/detail/TypeList.h(177): error C2664: 'luabridge::detail::TypeListValues<luabridge::detail::TypeList<Param,luabridge::detail::TypeList<const SRect&,luabridge::detail::MakeTypeList<>::Result>>>::TypeListValues(luabridge::detail::TypeListValues<luabridge::detail::TypeList<Param,luabridge::detail::TypeList<const SRect&,luabridge::detail::MakeTypeList<>::Result>>> &&)': cannot convert argument 1 from 'std::vector<SRect,std::allocator<_Ty>>' to 'Head'
1>        with
1>        [
1>            Param=SRectVector *
1>        ]
1>        and
1>        [
1>            _Ty=SRect
1>        ]
1>        and
1>        [
1>            Head=SRectVector *
1>        ]
1>E:\Code\include\LuaBridge/detail/TypeList.h(179): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>E:\Code\include\LuaBridge/detail/TypeList.h(176): note: while compiling class template member function 'luabridge::detail::ArgList<Params,1>::ArgList(lua_State *)'
EN

回答 2

Stack Overflow用户

发布于 2020-11-09 15:57:01

经过进一步挖掘,我找到了原因。一个完整的用户定义类可以很容易地导出。但容器指针不是。在我添加了这样的代码后编译正常,

代码语言:javascript
复制
namespace LuaBridge
{
    template <>
    struct Stack <SRectVector*>
    {
        static void push(lua_State* L, SRectVector* ptr)
        {
            SRectVector** pp = (SRectVector**)lua_newuserdata(L, sizeof(SRectVector*));
            *pp = ptr;
        }

        static SRectVector* get(lua_State* L, int index)
        {
            return (SRectVector*)lua_touserdata(L, index);
        }
    };
}

或者添加一个更通用的,

代码语言:javascript
复制
template <class T>
struct Stack <std::vector<T>*>
{
    typedef typename std::vector<T>* ContainerPointerType;

    static void push(lua_State* L, ContainerPointerType ptr)
    {
        ContainerPointerType* pp = (ContainerPointerType*)lua_newuserdata(L, sizeof(ContainerPointerType));
        *pp = ptr;
    }

    static ContainerPointerType get(lua_State* L, int index)
    {
        return (ContainerPointerType)lua_touserdata(L, index);
    }
};
票数 0
EN

Stack Overflow用户

发布于 2020-11-16 16:19:58

不要包含,然后解决这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64710979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档