Hi,
Could someone help me out here? I'm using luabind for the first time and despite that fact that my little test app works fine, I get a weird error message at the very end of the program (whilst returning 0). Am I missing something?
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#include <luabind\luabind.hpp>
#include <luabind\adopt_policy.hpp>
#include <iostream>
using namespace std;
using namespace luabind;
lua_State* L_;
class baseclass
{
public:
baseclass(const char* s) { std::cout << s << "\n"; }
virtual void f(int a) { std::cout << "-f(" << a << ")\n"; }
};
struct baseclass_wrapper: baseclass
{
luabind::object m_l;
baseclass_wrapper(luabind::object l, const char* s): baseclass(s), m_l(l) {}
virtual void f(int a) { call_member<void>(m_l, "f", a); }
static void f_static(baseclass* ptr, int a)
{
return ptr->baseclass::f(a);
}
};
int main()
{
// Open Lua
L_ = lua_open();
// Prepare Luabind
luabind::open(L_);
luabind::module(L_)
[
class_<baseclass, baseclass_wrapper>("baseclass")
.def(constructor<const char*>())
.def("f", &baseclass_wrapper::f_static)
];
// Load required Lua libraries (optional)
lua_baselibopen(L_);
lua_iolibopen(L_);
lua_strlibopen(L_);
lua_mathlibopen(L_);
// Invoke script (error handling omitted)
lua_dofile(L_, "test.lua");
/*
class 'derived' (baseclass)
function derived:__init() super('derived name')
end
function derived:f(num)
print('overwritten');
end
function derived:print()
print(30)
end
*/
lua_dofile(L_, "test2.lua");
/*
d = derived('Hello');
d:print();
d:f(3);
*/
luabind::object my_class(luabind::get_globals(L_)["derived"]);
baseclass* p = luabind::object_cast<baseclass*>(my_class(), adopt(return_value));
p->f(1);
delete p;
// Close Lua (Luabind shutsdown automatically)
lua_close(L_);
cin.get();
return 0;
}
It seems to be connected somehow to the following statements although they do produce the desired effect.
luabind::object my_class(luabind::get_globals(L_)["derived"]);
baseclass* p = luabind::object_cast<baseclass*>(my_class(), adopt(return_value));
Many Thanks