[英]problems with lua_register C++
I am having an issue with using lua_register function to put my C/C++ functions into Lua. 我在使用lua_register函数将C / C ++函数放入Lua时遇到问题。 Here is the code I have for my project: 这是我的项目代码:
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);
}
}
}
here is the header file: 这是头文件:
#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;
};
also here is the error: 这也是错误:
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
also with the intelliSense: 也与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
So all together I was able to get Lua to run just fine with C++ and I was able to call lua scripts in C++. 因此,总的来说,我能够使Lua在C ++上正常运行,并且能够在C ++中调用lua脚本。 The only problem is trying to call C functions inside the lua scripts. 唯一的问题是试图在lua脚本中调用C函数。 I am really stuck and thank you in advance! 我真的很困惑,在此先感谢您!
Your Print Function has the wrong type. 您的打印功能类型错误。
you should get the same error from 你应该从得到同样的错误
lua_CFunction f{ Print };
f
has the correct type here. f
在这里具有正确的类型。 Rewrite your print to fit that type. 重写打印以适合该类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.