使用 LuaBridge 导出容器类
2020-11-6 8:14:38
收藏:0
阅读:102
评论:2
我想将 C++ 中的 SRect 和 SRectVector 导出到 Lua,但编译失败。 请问正确的方法是什么? 编译器:vs2019,vc++11 操作系统:Win10 64
Push() 报编译错误,
我认为参数只是 SRectVector *,为什么编译器认为它是 'std::vector<SRect,std::allocator<_Ty>>'?
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>>> &&)': 无法将参数 1 从 'std::vector<SRect,std::allocator<_Ty>>' 转换为 '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: 没有可用的用户定义的转换运算符,可以执行此转换,或者该运算符无法调用
1>E:\Code\include\LuaBridge/detail/TypeList.h(176): note: 正在编译类模板成员函数 'luabridge::detail::ArgList<Params,1>::ArgList(lua_State *)'
点赞
用户12098220
不要包含 <LuaBridge/Vector.h>,然后解决这个问题。
2020-11-16 08:19:58
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

经过更深入的挖掘,我找到了原因。一个已完成的用户自定义类可以很容易地导出(export)。但是指向容器的指针却不可以。我加入了以下代码后,编译通过了:
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); } }; }或者加入更通用的代码:
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); } };