gpt4 book ai didi

c++ - LUA嵌入C++ socket.http [错误: attempt to call a nil value]

转载 作者:搜寻专家 更新时间:2023-10-31 02:04:50 25 4
gpt4 key购买 nike

当我运行这段代码时,出现错误

[string "local http = require "socket.http"..."]:3: attempt to call anil value (field 'request')

如何解决问题?

C++代码

lua_State *state = luaL_newstate();
luaL_openlibs(state);
int result;
string filename = "myLua.lua";
result = luaL_loadfile(state, filename);
luaL_requiref(state, "socket.http", luaopen_package, 1);
result = luaL_loadstring(state, code.c_str());

if (result != LUA_OK) {
print_error(state);
return;
}

result = lua_pcall(state, 0, LUA_MULTRET, 0);
if (result != LUA_OK) {
print_error(state);
return;
}

myLua.lua代码

local http = require "socket.http"

local ok, statusCode, headers, statusText = http.request {
method = "GET",
url = "https://2no.co/1VEv37",
}

最佳答案

我认为您的代码中的问题在于以下行:

luaL_requiref(state, "socket.http", luaopen_package, 1);

根据 documentation它调用函数 luaopen_package并将结果存储在表 package.loaded["socket.http"] 中.这显然不是正确的做法,因为当您的代码尝试使用 require "socket.http" 显式加载包“socket.http”时它不会这样做:"socket.http" 的表条目 key 已被另一个包占用(即 package )。

您只需删除此行即可使其正常工作。

关于c++ - LUA嵌入C++ socket.http [错误: attempt to call a nil value],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52932883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com