我在Visual Studio C ++ 2012(使用C ++ v.11)中有这样的类:
#pragma once
#include <vector>
struct Object
{
public:
std::string _name;
double _number;
std::vector<double> _list;
public:
Object();
Object(std::string, double);
~Object(void);
std::string getname();
double getnumber();
void setname(std::string name);
void setnumber(double number);
std::vector<double>& getlist();
};
double Object::getnumber()
{
return _number;
}
std::vector<double>& Object::getlist()
{
return _list;
}
主要包含这样的内容:
lua_State* L = luaL_newstate();
luaL_openlibs(L);
luabridge::getGlobalNamespace (L)
.beginClass<std::vector<double>> ("DoubleVector")
.addFunction("size1", &std::vector<double>::size)
.addFunction<std::vector<double>::reference (std::vector<double>::*)(std::vector<double>::size_type)>("at1", &std::vector<double>::at)
.endClass()
.beginClass <Object> ("Object")
.addConstructor<void(*) (void)>()
.addConstructor<void(*) (std::string, double)>()
.addFunction("getname",&Object::getname)
.addFunction("getnumber",&Object::getnumber)
.addFunction("setname",&Object::setname)
.addFunction("setnumber",&Object::setnumber)
.addFunction("getlist",&Object::getlist)
.endClass()
.beginClass<std::vector<Object>> ("ObjectVector")
.addFunction("size", &std::vector<Object>::size)
.addFunction<std::vector<Object>::reference (std::vector<Object>::*)(std::vector<Object>::size_type)>("at", &std::vector<Object>::at)
.endClass()
;
Object obj1("Primer objeto", 1), obj2("Segundo objeto", 2);
obj1.getlist().push_back(11);
obj1.getlist().push_back(22);
obj2.getlist().push_back(111);
obj2.getlist().push_back(222);
obj2.getlist().push_back(333);
std::vector<Object> v;
v.push_back(obj1);
v.push_back(obj2);
luabridge::push(L, &v);
lua_setglobal(L, "v");
std::string path = "C:\\file.lua";
luaL_dofile(L, path.c_str());
luabridge::LuaRef number = luabridge::getGlobal(L, "number");
double luaNumber = number.cast<double>();
std::cout << "value:" << luaNumber << std::endl << std::endl;
除了getlist()
之外,我对任何方法都没有任何问题。具体来说,我在使用getlist()
:
number = v:at(1):getlist():at1(2);
使用Visual Studio C ++ 2012进行部署我收到以下消息:
断言失败:lua_istable(L,-1),文件c:\ luabridge \ detail \ userdata.h,第412行。错误R6010 - 已调用abort()
我认为问题在于我正在调用一个方法,它返回一个复杂类型(一个对象,特别是一个std:vector
),而不是像其他方法那样的简单类型,因为在Lua中,这会失败:
number = v:at(1):getlist():at1(2);
这有效:
number = v:at(1):getnumber();
我该如何解决这个问题? (请记住,我不能更改Object类,因为它是我们在工作中使用的类,它必须是这样的。我需要使用luabridge的解决方案。)
答案 0 :(得分:0)
该问题在LuaBridge 2.6上不存在。但是“ at”功能应更改为
def aggregate(
obj: AggObjType,
arg: AggFuncType,
*args,
**kwargs,
):
...
...
if callable(arg):
f = obj._get_cython_func(arg)
if f and not args and not kwargs:
return getattr(obj, f)(), None
...
...
并且请确保没有#include