首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LuaBridge断言错误

LuaBridge断言错误
EN

Stack Overflow用户
提问于 2014-08-04 13:47:58
回答 1查看 1.2K关注 0票数 0

为了从lua访问c++,我已经阅读了一些有关luabridge的教程,但是我遇到了一个问题,我似乎无法通过搜索google找到答案。

我已经设置了一个示例程序来运行脚本并从c++访问lua,这很好。但是,当我尝试在全局命名空间中注册一个函数时,它会在运行时抱怨--编译很好。

代码语言:javascript
复制
#include <iostream>
#include <string>

#include "../LuaBridge/LuaBridge.h"

using namespace luabridge;

void printMessage(const std::string& s) {
    std::cout << s << std::endl;
}

int main() {
    lua_State* L = luaL_newstate();

    getGlobalNamespace(L).addFunction("printMessage", printMessage);

    luaL_dofile(L, "script.lua");
    luaL_openlibs(L);
    lua_pcall(L, 0, 0, 0);
    LuaRef s = getGlobal(L, "testString");
    LuaRef n = getGlobal(L, "number");
    std::string luaString = s.cast<std::string>();
    int answer = n.cast<int>();
    std::cout << luaString << std::endl;
    std::cout << "And here's our number:" << answer << std::endl;
}

因此,这段带有addFunction调用的代码给出了这个错误

代码语言:javascript
复制
Lua: /home/linuxxon/Programming/kingdoms-online/Script/Lua/../LuaBridge/detail/Namespace.h:1080: luabridge::Namespace& luabridge::Namespace::addFunction(const char*, FP) [with FP = void (*)(const std::basic_string<char>&)]: Assertion `(lua_type(L, (-1)) == 5)' failed.

通过addFunction调用,我得到了对脚本的期望。

也许有什么明显的事情我已经错过了,因为我没有发现任何类似的东西?

我将非常感谢所有的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-05 15:56:31

问题是

代码语言:javascript
复制
luaL_openlibs(L);

是在脚本运行之后。问题是在我遵循的教程中,我在开始尝试lua时遇到了同样的事情。

在创建了新的lua状态之后,所有这些都通过调用它来完美地工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25120227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档