使用C+访问Lua全局表+; 返回< /Calp>语句。您如何在C++中迭代或拥有该表的副本?您已经有了迭代部分。您只是不需要在Lua堆栈上迭代表。在运行循环之前,使用lua_getglobal()获取表。您能在更新1中的帖子中为我检查一下吗。我更新了代码。但是也有一些问题,它说有事情要做的代码将为nil,而程序会突然访问一个全局变量dolua_getglobal(L,“问题”)。但是你的代码有更多的问题。您的表是一个数组数组,代码尝试迭代由键和值组成的简单表。查看另一个问题以供参考 int main() { lua_State *lua_state = luaL_newstate(); luaL_openlibs(lua_state); luaL_loadfile(lua_state, "main.lua"); lua_getglobal(lua_state, "problem"); //lua_pushglobaltable(lua_state); // Get global table lua_pushnil(lua_state); // put a nil key on stack while (lua_next(lua_state, -2) != 0) { // key(-1) is replaced by the next key(-1) in table(-2) std::string name = lua_tostring(lua_state, -2); // Get key(-2) name std::cout << name << std::endl; lua_pop(lua_state, 1); // remove value(-1), now key on top at(-1) } lua_pop(lua_state, 1); // remove global table(-1) lua_call(lua_state, 0, 0); return 0; } problem = { {9, 0, 0, 1, 0, 0, 0, 0, 5}, {0, 0, 5, 0, 9, 0, 2, 0, 1}, {8, 0, 0, 0, 4, 0, 0, 0, 0}, {0, 0, 0, 0, 8, 0, 0, 0, 0}, {0, 0, 0, 7, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 2, 6, 0, 0, 9}, {2, 0, 0, 3, 0, 0, 0, 0, 6}, {0, 0, 0, 2, 0, 0, 9, 0, 0}, {0, 0, 1, 9, 0, 4, 5, 7, 0}, } print("Lua Works") user_input = io.read();
更新1使用C+访问Lua全局表+; 返回< /Calp>语句。您如何在C++中迭代或拥有该表的副本?您已经有了迭代部分。您只是不需要在Lua堆栈上迭代表。在运行循环之前,使用lua_getglobal()获取表。您能在更新1中的帖子中为我检查一下吗。我更新了代码。但是也有一些问题,它说有事情要做的代码将为nil,而程序会突然访问一个全局变量dolua_getglobal(L,“问题”)。但是你的代码有更多的问题。您的表是一个数组数组,代码尝试迭代由键和值组成的简单表。查看另一个问题以供参考 int main() { lua_State *lua_state = luaL_newstate(); luaL_openlibs(lua_state); luaL_loadfile(lua_state, "main.lua"); lua_getglobal(lua_state, "problem"); //lua_pushglobaltable(lua_state); // Get global table lua_pushnil(lua_state); // put a nil key on stack while (lua_next(lua_state, -2) != 0) { // key(-1) is replaced by the next key(-1) in table(-2) std::string name = lua_tostring(lua_state, -2); // Get key(-2) name std::cout << name << std::endl; lua_pop(lua_state, 1); // remove value(-1), now key on top at(-1) } lua_pop(lua_state, 1); // remove global table(-1) lua_call(lua_state, 0, 0); return 0; } problem = { {9, 0, 0, 1, 0, 0, 0, 0, 5}, {0, 0, 5, 0, 9, 0, 2, 0, 1}, {8, 0, 0, 0, 4, 0, 0, 0, 0}, {0, 0, 0, 0, 8, 0, 0, 0, 0}, {0, 0, 0, 7, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 2, 6, 0, 0, 9}, {2, 0, 0, 3, 0, 0, 0, 0, 6}, {0, 0, 0, 2, 0, 0, 9, 0, 0}, {0, 0, 1, 9, 0, 4, 5, 7, 0}, } print("Lua Works") user_input = io.read();,c++,lua,global-variables,C++,Lua,Global Variables,更新1 int main() { lua_State *lua_state = luaL_newstate(); luaL_openlibs(lua_state); luaL_loadfile(lua_state, "main.lua"); lua_getglobal(lua_state, "problem"); //lua_pushglobaltable(lua_state); // Get global table lua_pu
int main()
{
lua_State *lua_state = luaL_newstate();
luaL_openlibs(lua_state);
luaL_loadfile(lua_state, "main.lua");
lua_getglobal(lua_state, "problem");
//lua_pushglobaltable(lua_state); // Get global table
lua_pushnil(lua_state); // put a nil key on stack
while (lua_next(lua_state, -2) != 0) { // key(-1) is replaced by the next key(-1) in table(-2)
std::string name = lua_tostring(lua_state, -2); // Get key(-2) name
std::cout << name << std::endl;
lua_pop(lua_state, 1); // remove value(-1), now key on top at(-1)
}
lua_pop(lua_state, 1); // remove global table(-1)
lua_call(lua_state, 0, 0);
return 0;
}
您没有任何值可在Lua堆栈上迭代。
intt=2代码>不会反映任何内容,脚本也不会返回要留在堆栈上的值。
请参阅关于访问全局表的例子。不是“变量”变量,它是存储在Lua全局表中的,它不在Lua堆栈中,直到调用LuaAuGeGlobal(),或者从脚本返回,使用“代码>返回< /Calp>语句。您如何在C++中迭代或拥有该表的副本?您已经有了迭代部分。您只是不需要在Lua堆栈上迭代表。在运行循环之前,使用lua_getglobal()获取表。您能在更新1中的帖子中为我检查一下吗。我更新了代码。但是也有一些问题,它说有事情要做的代码将为nil,而程序会突然访问一个全局变量dolua_getglobal(L,“问题”)代码>。但是你的代码有更多的问题。您的表是一个数组数组,代码尝试迭代由键和值组成的简单表。查看另一个问题以供参考
int main()
{
lua_State *lua_state = luaL_newstate();
luaL_openlibs(lua_state);
luaL_loadfile(lua_state, "main.lua");
lua_getglobal(lua_state, "problem");
//lua_pushglobaltable(lua_state); // Get global table
lua_pushnil(lua_state); // put a nil key on stack
while (lua_next(lua_state, -2) != 0) { // key(-1) is replaced by the next key(-1) in table(-2)
std::string name = lua_tostring(lua_state, -2); // Get key(-2) name
std::cout << name << std::endl;
lua_pop(lua_state, 1); // remove value(-1), now key on top at(-1)
}
lua_pop(lua_state, 1); // remove global table(-1)
lua_call(lua_state, 0, 0);
return 0;
}
problem =
{
{9, 0, 0, 1, 0, 0, 0, 0, 5},
{0, 0, 5, 0, 9, 0, 2, 0, 1},
{8, 0, 0, 0, 4, 0, 0, 0, 0},
{0, 0, 0, 0, 8, 0, 0, 0, 0},
{0, 0, 0, 7, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 2, 6, 0, 0, 9},
{2, 0, 0, 3, 0, 0, 0, 0, 6},
{0, 0, 0, 2, 0, 0, 9, 0, 0},
{0, 0, 1, 9, 0, 4, 5, 7, 0},
}
print("Lua Works")
user_input = io.read();