Hey guys,
I'm reasonably new to C++ coding (previously a C# developer) and am running into what is probably a fairly basic problem that's generating compiler errors, but I'm unable to understand it myself. Basically I'm attempting to implement the Singleton pattern for a class (a class that will be used as a Lua interpreter in a game project), and am getting the following errors:
The LuaEngine.h file:
The LuaEngine.cpp file:
I realise that I might have other problems (such as I shouldn't be using a Singleton, or I'm implementing it wrong, or whatever) and would appreciate advice on that topic as well, but even with that I'd prefer if you could explain the source of my errors so that I can avoid them on future unrelated classes as well
.
FYI this is not a homework assignment (I work full time) but it is my first experience of trying to write my own code in C++ moving from C# with the XNA framework most recently. This code was based on the Singleton Implementation in C++ available at http://www.codeproject.com/Articles/1921/Singleton-Pattern-its-implementation-with-C
I'm reasonably new to C++ coding (previously a C# developer) and am running into what is probably a fairly basic problem that's generating compiler errors, but I'm unable to understand it myself. Basically I'm attempting to implement the Singleton pattern for a class (a class that will be used as a Lua interpreter in a game project), and am getting the following errors:
1>LuaEngine.obj : error LNK2001: unresolved external symbol "private: static bool LuaEngine::instanceFlag" (?instanceFlag@LuaEngine@@0_NA) 1>LuaEngine.obj : error LNK2001: unresolved external symbol "private: static class LuaEngine * LuaEngine::lua" (?lua@LuaEngine@@0PAV1@A) 1>LuaEngine.obj : error LNK2001: unresolved external symbol "private: static struct lua_State * LuaEngine::luaInterpreter" (?luaInterpreter@LuaEngine@@0PAUlua_State@@A) 1>D:\Development\Test\LUAEngine\Debug\LUAEngine.exe : fatal error LNK1120: 3 unresolved externals
The LuaEngine.h file:
#include "LuaGeneral.h"
using namespace std;
class LuaEngine
{
private:
static bool instanceFlag;
static LuaEngine* lua;
static lua_State* luaInterpreter;
LuaEngine();
public:
static LuaEngine* getLuaInstance();
~LuaEngine();
};
The LuaEngine.cpp file:
/*
Singleton for use with Lua interpreter.
*/
#include "LuaEngine.h"
LuaEngine::LuaEngine() {
luaInterpreter = lua_open();
luaL_openlibs(luaInterpreter);
lua = this;
}
LuaEngine::~LuaEngine() {
lua_close(luaInterpreter);
instanceFlag = false;
}
LuaEngine* LuaEngine::getLuaInstance() {
if (!instanceFlag)
{
lua = new LuaEngine();
instanceFlag = true;
return lua;
}
else
return lua;
}
I realise that I might have other problems (such as I shouldn't be using a Singleton, or I'm implementing it wrong, or whatever) and would appreciate advice on that topic as well, but even with that I'd prefer if you could explain the source of my errors so that I can avoid them on future unrelated classes as well
FYI this is not a homework assignment (I work full time) but it is my first experience of trying to write my own code in C++ moving from C# with the XNA framework most recently. This code was based on the Singleton Implementation in C++ available at http://www.codeproject.com/Articles/1921/Singleton-Pattern-its-implementation-with-C