Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
luabind:无法访问全局变量 我有一个C++类,我想通过全局变量来访问Lua脚本,但是当我尝试使用它时,我得到以下错误: terminate called after throwing an instance of 'luabind::error' what(): lua runtime error baz.lua:3: attempt to index global 'foo' (a nil value)Aborted (core dumped)_C++_Lua_Luabind - Fatal编程技术网

luabind:无法访问全局变量 我有一个C++类,我想通过全局变量来访问Lua脚本,但是当我尝试使用它时,我得到以下错误: terminate called after throwing an instance of 'luabind::error' what(): lua runtime error baz.lua:3: attempt to index global 'foo' (a nil value)Aborted (core dumped)

luabind:无法访问全局变量 我有一个C++类,我想通过全局变量来访问Lua脚本,但是当我尝试使用它时,我得到以下错误: terminate called after throwing an instance of 'luabind::error' what(): lua runtime error baz.lua:3: attempt to index global 'foo' (a nil value)Aborted (core dumped),c++,lua,luabind,C++,Lua,Luabind,我的Lua脚本(baz.Lua)如下所示: ——baz.lua 帧=0 bar=foo:createBar() 函数baz() 帧=帧+1 条:setText(“帧:…帧”) 结束 我制作了一个简单而简短(尽我所能)的main.cpp,它重现了这个问题: #包括 #包括 外部“C”{ #包括“lua.h” #包括“lualib.h” #包括“lauxlib.h” } #包括 #包括 分类栏 { 公众: 静态void init(lua_State*L) { 使用luabind::module;

我的Lua脚本(baz.Lua)如下所示:

——baz.lua
帧=0
bar=foo:createBar()
函数baz()
帧=帧+1
条:setText(“帧:…帧”)
结束
我制作了一个简单而简短(尽我所能)的main.cpp,它重现了这个问题:

#包括
#包括
外部“C”{
#包括“lua.h”
#包括“lualib.h”
#包括“lauxlib.h”
}
#包括
#包括
分类栏
{
公众:
静态void init(lua_State*L)
{
使用luabind::module;
使用luabind::class\ux;
模块(L)
[
类别("酒吧")
.def(“setText”,&bar::setText)
];
}
void setText(const std::string和text)
{

std::cout在注册任何全局变量之前,您正在运行
baz.lua
。在注册绑定之后放置
dofile
命令

顺序如下:

    调用C++中的FoO构造函数,
  • 创建Lua状态
  • 运行
    lua.baz
  • 注册您的绑定
  • 然后在C++中调用f.bz。
g++ -std=c++11 -llua -lluabind main.cpp