views:

211

answers:

1

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.

+1  A: 

I would encourage you to get this started with the binaries and sample VS2008 solution available from this website. It has the exact same sample code you are trying to run (minus the typos) and it worked well on my machine. If it still doesn't work, you'll need help from the Lua community. A minidump is probably required to help them diagnose this, just the exception message isn't enough.

Hans Passant
I'd say, more specifically, Luabind community. Welcome to the mailing list: https://lists.sourceforge.net/lists/listinfo/luabind-user
Alexander Gladysh
What typos are you talking about?
Overv
The ones in the linked blog post.
Hans Passant