Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.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
Lua 5.2版:';尝试调用nil值';来自lua_pcall 我在获取Lua 5.2函数时,遇到了问题,从C++调用。_C++_Lua_Lua 5.2 - Fatal编程技术网

Lua 5.2版:';尝试调用nil值';来自lua_pcall 我在获取Lua 5.2函数时,遇到了问题,从C++调用。

Lua 5.2版:';尝试调用nil值';来自lua_pcall 我在获取Lua 5.2函数时,遇到了问题,从C++调用。,c++,lua,lua-5.2,C++,Lua,Lua 5.2,这是Lua块(名为test.Lua): 这就是C++: int iErr = 0; //Create a lua state lua_State *lua = luaL_newstate(); // Load io library luaopen_io (lua); //load the chunk we want to execute (test.lua) iErr = luaL_loadfile(lua, "test.lua"); if (iErr == 0) { printf

这是Lua块(名为test.Lua):

这就是C++:

int iErr = 0;

//Create a lua state
lua_State *lua = luaL_newstate();

// Load io library
luaopen_io (lua);

//load the chunk we want to execute (test.lua)
iErr = luaL_loadfile(lua, "test.lua");
if (iErr == 0) {
    printf("successfully loaded test.lua\n");

    // Push the function name onto the stack
    lua_getglobal(lua, "testFunction");
    printf("called lua_getglobal. lua stack height is now %d\n", lua_gettop(lua));

    //Call our function
    iErr = lua_pcall(lua, 0, 0, 0);
    if (iErr != 0) {
        printf("Error code %i attempting to call function: '%s'\n", iErr, lua_tostring(lua, -1));
    }

} else {
    printf("Error loading test.lua. Error code: %s\n", lua_tostring(lua, -1));        
}
lua_close (lua);
当我跟踪时,我看到它很好地加载了test.lua脚本(没有返回错误),然后在使用函数名调用lua_getglobal之后,它显示堆栈高度为3

但是,它在lua_pcall失败,错误代码为2:“尝试调用nil值”

我已经阅读了大量Lua5.2代码的示例,似乎看不出哪里出了问题。 这看起来肯定会起作用(根据我所读到的)

我检查了拼写和区分大小写的能力,结果都吻合


我误解了什么吗?

luaL\u loadfile
只加载文件,而不运行它。请尝试
luaL\u dofile

您仍然会得到一个错误,因为
print
是在基本库中定义的,而不是在io库中定义的。因此,请改为调用
luaopen\u base

您需要在
lua\u getglobal()之前调用“
启动lua\u pacll()
”。请参阅。整个代码应该是这样的:

int iErr = 0;

//Create a lua state
lua_State *lua = luaL_newstate();

// Load base library
luaopen_base (lua);

//load the chunk we want to execute (test.lua)
iErr = luaL_loadfile(lua, "test.lua");
if (iErr == 0) {
    printf("successfully loaded test.lua\n");

    //Call priming lua_pcall
    iErr = lua_pcall(lua, 0, 0, 0);
    if (iErr != 0) {
        printf("Error code %i attempting to call function: '%s'\n", iErr, lua_tostring(lua, -1));
    }

    // Push the function name onto the stack
    lua_getglobal(lua, "testFunction");
    printf("called lua_getglobal. lua stack height is now %d\n", lua_gettop(lua));

    //Call our function
    iErr = lua_pcall(lua, 0, 0, 0);
    if (iErr != 0) {
        printf("Error code %i attempting to call function: '%s'\n", iErr, lua_tostring(lua, -1));
    }

} else {
    printf("Error loading test.lua. Error code: %s\n", lua_tostring(lua, -1));        
}
lua_close (lua);

打印功能未加载<代码> LalalOpenOpenLBS 应该加载它。谢谢,我现在已经工作了,已经按照我的建议修改了C++。我现在了解到,要注册块中的函数,它必须是“运行”的,而不仅仅是Lua术语中的“加载”。谢谢你的回答@user2795503如果答案解决了问题,不要忘记“接受”答案。
int iErr = 0;

//Create a lua state
lua_State *lua = luaL_newstate();

// Load base library
luaopen_base (lua);

//load the chunk we want to execute (test.lua)
iErr = luaL_loadfile(lua, "test.lua");
if (iErr == 0) {
    printf("successfully loaded test.lua\n");

    //Call priming lua_pcall
    iErr = lua_pcall(lua, 0, 0, 0);
    if (iErr != 0) {
        printf("Error code %i attempting to call function: '%s'\n", iErr, lua_tostring(lua, -1));
    }

    // Push the function name onto the stack
    lua_getglobal(lua, "testFunction");
    printf("called lua_getglobal. lua stack height is now %d\n", lua_gettop(lua));

    //Call our function
    iErr = lua_pcall(lua, 0, 0, 0);
    if (iErr != 0) {
        printf("Error code %i attempting to call function: '%s'\n", iErr, lua_tostring(lua, -1));
    }

} else {
    printf("Error loading test.lua. Error code: %s\n", lua_tostring(lua, -1));        
}
lua_close (lua);