lua_register在C++中存在问题。
2015-7-13 1:45:6
收藏:0
阅读:121
评论:1
我在使用lua_register函数将我的C/C++函数放入Lua时遇到了问题。以下是我项目的代码:
LuaCore::LuaCore()
{
L = luaL_newstate();
luaopen_io(L);
luaopen_base(L);
luaopen_table(L);
luaopen_string(L);
luaopen_math(L);
luaL_openlibs(L);
}
LuaCore::~LuaCore()
{
if (L != NULL)
{
lua_close(L);
}
}
void LuaCore::reportErrors(lua_State *l, int status)
{
if (status != 0)
{
agk::Message(lua_tostring(L, -1));
lua_pop(L, 1);
}
}
bool LuaCore::executeFile(const char* f)
{
int s = luaL_loadfile(L, f);
if (s == 0)
{
s = lua_pcall(L, 0, LUA_MULTRET, 0);
}
reportErrors(L, s);
return true;
}
void LuaCore::loadFunctions()
{
lua_register(L, "Print", Print);
}
void LuaCore::Print(lua_State *L)
{
int argc = lua_gettop(L);
for (int n = 1; n <= argc; ++n)
{
if (n > 2)
{
std::cout << lua_tostring(L,n);
}
}
}
以下是头文件:
#pragma once
extern "C"
{
#include "lua_lib\lua-5.3.1\src\lua.hpp"
}
#include <cstdlib>
#include <cstring>
#include <string>
#include <iostream>
using namespace std;
class LuaCore
{
public:
LuaCore();
~LuaCore();
bool executeFile(const char* f);
void reportErrors(lua_State *l, int status);
void loadFunctions();
void Print(lua_State *l);
lua_State *L;
};
同时这是错误:
Error 1 error C2664: 'void lua_pushcclosure(lua_State
*,lua_CFunction,int)' : cannot convert argument 2 from 'void (__thiscall
LuaCore::* )(lua_State *)' to 'lua_CFunction' C:\Program Files
(x86)\The Game Creators\AGK2\Tier 2\apps\EasyCoreGamer_\LuaCore.cpp 46
1 Template
并且还有intelliSense:
2 IntelliSense: argument of type "void (LuaCore::*)(lua_State *L)"
is incompatible with parameter of type "lua_CFunction" c:\Program
Files (x86)\The Game Creators\AGK2\Tier
2\apps\EasyCoreGamer_\LuaCore.cpp 46 2 Template
我成功地使用了Lua运行C++的代码,并且能够在C++中调用Lua脚本。唯一的问题是尝试在lua脚本中调用C函数。我真的卡住了,谢谢您提前帮助!
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

你的打印函数类型错误。
你应该从下面的代码中获得相同的错误:
lua_CFunction f{ Print };在这里,
f的类型是正确的。重写你的打印函数以适应该类型。