I'm trying to export a simple class to Lua using LuaBind. I took the code from two sites which showed roughly the same way to do it, but it's still failing.
// Default headers
#include <iostream>
#include <string>
// Lua headers
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#include "luabind/luabind.hpp"
// Sample class
class NumberPrinter
{
public:
NumberPrinter( int number ) : m_number( number ) {}
void print() { std::cout << m_number << "\n"; }
private:
int m_number;
};
int main() {
// Create Lua state and load sample file
lua_State *luaState = lua_open();
luabind::open( luaState );
// Set up bind to number class
luabind::module( luaState ) [
luabind::class_<NumberPrinter>( "NumberPrinter" )
.def( luabind::constructor<int>() )
.def( "print", &NumberPrinter::print )
];
// Use the class in Lua
luaL_dostring( luaState,
"Print2000 = NumberPrinter(2000)\n"
"Print2000:print()\n"
);
// Clean up Lua state
lua_close( luaState );
getchar();
return 0;
}
When running that code, luabind::module causes the following runtime error and has no other information in debug mode:
Unhandled exception at 0x690008f5 in Lua Playground.exe: 0xC0000005: Access violation.