【问题标题】:Access violation when exporting a C++ class to Lua using LuaBind使用 LuaBind 将 C++ 类导出到 Lua 时访问冲突
【发布时间】:2010-12-29 19:09:29
【问题描述】:

我正在尝试使用 LuaBind 将一个简单的类导出到 Lua。我从两个站点获取了代码,它们显示了大致相同的方法,但仍然失败。

// 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;
}

运行该代码时,luabind::module 会导致以下运行时错误,并且在调试模式下没有其他信息:

在 0x690008f5 处未处理的异常 Lua Playground.exe:0xC0000005:访问 违规。

【问题讨论】:

    标签: c++ class lua export luabind


    【解决方案1】:

    我鼓励您从this website 提供的二进制文件和示例 VS2008 解决方案开始。它具有您尝试运行的完全相同的示例代码(减去拼写错误),并且在我的机器上运行良好。如果它仍然不起作用,您将需要 Lua 社区的帮助。可能需要一个小型转储来帮助他们诊断这一点,只是异常消息是不够的。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多