0
私は2つのファイルを持っています - 1つはLuaスクリプトとスクリプト自体を実行するものです。ここでいくつかのデータをLua呼び出し関数にプッシュ
は、彼らは以下のとおりです。
host.cpp:
#include <lua.hpp>
#include <iostream>
using namespace std;
int someHandler(lua_State *l)
{
int argc = lua_gettop(l);
for (int i = 0; i < argc; i++)
{
cout << "ARG[" << i + 1 << "] = " << lua_tostring(l, i + 1) << endl;
}
lua_pushstring(l, "m_pi");
//lua_pop(l, argc - 1);
//lua_pushnumber(l, 3.14);
return argc;
}
int main()
{
lua_State *l = lua_open();
luaL_openlibs(l);
lua_register(l, "moo", someHandler);
luaL_dofile(l, "script.lua");
lua_close(l);
return 0;
}
script.lua:
res = moo("hello", "world");
print(moo());
for k, v in res do
print(k.." = "..v);
end
はg++ host.cpp -o host.elf -I/usr/include/lua5.1 -llua5.1
でhost.cpp
をコンパイル。
host.elf
の実行結果は次のとおりです。
ARG[1] = hello
ARG[2] = world
<\n>
それがされるべきである:
ARG[1] = hello
ARG[2] = world
m_pi
私は間違って何をしますか?
あなたが '帰国してはいけません1' 'argc'の代わりに' 2'ですか? –
'argc'は' moo() 'の場合は' 0'ですが、ポイントは立っています。 – Mankarse
(for the Lua)は何を意味するのですか? 'res'は文字列であり、有効なイテレータコンストラクタではありません。 – Mankarse