2
我有一個關於在C++中使用的這個簡單的LUA腳本代碼的問題。Lua在C++奇怪的行爲,我不明白?
Main.cpp
#include <iostream>
using namespace std;
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
int main()
{
lua_State *L=luaL_newstate();
luaL_openlibs(L);
luaL_loadfile(L,"script.lua");
lua_call(L,0,1);
int n;
n=lua_tonumber(L,-1); // Can be changed to 0 and i gain the same result as -1
cout<<n<<endl;
lua_close(L);
cout<<"test"<<endl;
return 0;
}
script.lua
print("Hello world")
return 10
程序是否正常工作,並返回10安慰,但是,問題是,爲什麼當我改變lua_tonumber(L,-1)-1到0它仍然返回10?看來,我有兩個10在堆棧之一與索引0和其他與索引-1。但爲什麼?
也許你的堆棧只有一個元素,所以索引0和索引-1指向同一個地方? – 2012-08-12 17:05:07
但是,當我改變腳本返回功能,例如「返回10,20」,那麼程序將返回-2爲10,因爲-1是20和0是20到。它看起來像-1和0索引指向相同的地方。 – 2012-08-12 17:11:04
它是錯誤還是我做錯了什麼? – 2012-08-12 17:12:02