lua程序设计里面的调用Lua函数的小例子 有个错误被我找到了

#include <windows.h>
#include <iostream>
using namespace std;


extern "C"
{
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
#include <iostream>

#pragma comment(lib,"E:\\luaSRC\\lua5.1\\lib\\static\\lua5.1.lib")
//1.lua
/*
function f (x, y)


--return (x^2 * math.sin(y))/(1 - x)
return x+y
end
*/

/* call a function `f' defined in Lua */

double f (lua_State* L, double x, double y)
{
	double z;

	luaL_dofile( L, "1.lua" );//书上的例子 用的luaL_loadfile 又没有pcall(0,0,0) 导致错误 找了半天

	/* push functions and arguments */

	lua_getglobal(L, "f");   /* function to be called */

	lua_pushnumber(L, x);    /* push 1st argument */

	lua_pushnumber(L, y);    /* push 2nd argument */


	/* do the call (2 arguments, 1 result) */

	if (lua_pcall(L, 2, 1, 0) != 0)

		luaL_error(L, "error running function `f': %s",

		lua_tostring(L, -1));

	/* retrieve result */

	if (!lua_isnumber(L, -1))

		luaL_error(L, "function `f' must return a number");

	z = lua_tonumber(L, -1);

	lua_pop(L, 1);  /* pop returned value */

	return z;
}



void main()
{
	lua_State* L = lua_open();

	luaL_openlibs(L);

	cout<<f(L, 1, 5);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值