0

I write a lua script and want to parse the script by c++ codes. In this script I have functions, and I want to get the function and save it for usage in the future. The main.cpp is like

#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

}

The P3626_PORT.lua is like:

lua_name = "P3626_PORT.lua"

run = function()  
    print (" this is my input!!!!!!!")
end 

stop = function() 
    print (" this is my output!!!!!!!")
end

The luaParser.h is like:

#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;

};

And at last, luaParser.cpp is like this:

#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();
}

When compiling the project, I get the error

error c2512: 'luabridge::LuaRef':no appropriate default constructor available

I have tried to solve this problem by such as initializing list but it doesn't work. Any idea how to solve this problem?

gladys0313
  • 2,569
  • 6
  • 27
  • 51
  • "I have tried to solve this problem by such as initializing list but it doesn't work." What happened? – Emil Laine Nov 24 '15 at 14:28
  • It pops out an error called "error C2248: 'luabridge::LuaRef::LuaRef' : cannot access private member declared in class 'luabridge::LuaRef'" – gladys0313 Nov 24 '15 at 14:31
  • Why are you trying to access a private member? Maybe take a look at the `LuaRef` documentation to see how they are supposed to be used. – Emil Laine Nov 24 '15 at 14:39
  • I just want to make it to be called only within LuaParser scope – gladys0313 Nov 24 '15 at 14:46

0 Answers0