I have a class like this in Visual Studio C++ 2012 (with 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;
}
And a main which contains something like this:
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;
I have no problems with any method except getlist()
. Specifically I have problems doing from Lua something like this with getlist()
:
number = v:at(1):getlist():at1(2);
Debugging with Visual Studio C++ 2012 I get the following message:
Assertion failed: lua_istable(L, -1), file c:\luabridge\detail\userdata.h, line 412. Error R6010 - abort() has been called
I think that the problem is that I am calling a method which returns a complex type (an object, specifically a std:vector
), and not a simple type like the other methods, because in Lua, this fails:
number = v:at(1):getlist():at1(2);
And this works:
number = v:at(1):getnumber();
How can I fix this? (Remember that I can't change the Object class because it's a class that we use at work an it must be like this. I need a solution using luabridge).
The issue does not exist on LuaBridge 2.6. But the "at" function should be changed to
And be sure do NOT have #include <LuaBridge/Vector.h>.