我编写了一个lua脚本,想要用c ++代码解析脚本。在这个脚本中我有函数,我想获得该函数并将其保存以供将来使用。 main.cpp
就像
#include <string>
#include <iostream>
#include "luaParser.h"
#include "LuaBridge.h"
extern "C" {
# include "lua.h"
# include "lauxlib.h"
# include "lualib.h"
}
using namespace luabridge;
void main (void)
{
lua_State* L = luaL_newstate();
if (luaL_dofile(L, "P3626_PORT.lua"))
{
printf("%s\n", lua_tostring(L, -1));
}
luaL_openlibs(L);
lua_pcall(L, 0, 0, 0);
LuaParser parser;
parser.luaParse(L); // get some values from the luaParser::luaParse function
lua_close(L);
parser.run(); // call the run function defined in LuaParser class
parser.stop(); // call the stop function defined in LuaParser class
}
P3626_PORT.lua
就像:
lua_name = "P3626_PORT.lua"
run = function()
print (" this is my input!!!!!!!")
end
stop = function()
print (" this is my output!!!!!!!")
end
luaParser.h
就像:
#pragma once
#include <string>
#include "LuaBridge.h"
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
using namespace luabridge;
class LuaParser
{
public:
LuaParser();
virtual ~LuaParser();
void luaParse(lua_State* L); // in this function, run (pcall) the script and retrieve functions
void run();
void stop();
private:
LuaRef mRun;
LuaRef mStop;
};
最后,luaParser.cpp
就像这样:
#ifdef _WIN32
#pragma warning(disable: 4786)
#endif
#include <stdlib.h>
#include <assert.h>
#include "LuaParser.h"
#include <iostream>
#include <string>
LuaParser::LuaParser(){}
LuaParser::~LuaParser(){}
void LuaParser::luaParse(lua_State* L)
{
using namespace luabridge;
LuaRef serviceName = getGlobal(L, "service_name");
std::string LuaServiceName = serviceName.cast<std::string>();
std::cout << LuaServiceName << std::endl;
// now Let's read the function
mRun = getGlobal(L, "run");
mStop = getGlobal(L, "stop");
}
void LuaParser::run()
{
mRun();
}
void LuaParser::stop()
{
mStop();
}
编译项目时,我收到错误
error c2512: 'luabridge::LuaRef':no appropriate default constructor available
我试图通过初始化列表来解决这个问题,但它不起作用。知道如何解决这个问题吗?