I'm following this singleton pattern, why error LNK2001: unresolved external symbol in this case? my problem LOOKs similar, but my issue is not with the definition of the static instance. My problem is is resolving the static GetInstance() definition from another class.
My errors seems different, or previous answers are inadequate. I've tried the suggestions, "You need to define s_instance outside the class" which doesn't make sense to me as a cpp noob. I declare the statics in the header, and define their implementation in the cpp as well.
I also don't need a lecture on thread safety of singletons, handler bindings use signals2...
State.h
class State
{
public:
State(void);
~State(void);
static State* instance;
static State* GetInstance();
...
};
State.cpp
State::AppState mCurrentState;
boost::signals2::signal<void ()> mSignal;
State* instance = NULL;
State* GetInstance()
{
if( instance == NULL)
{
instance = new State();
return instance;
}
else
{
return instance;
}
}
All that compiles fine. Then when I try to access the singleton like this
State *state = State::GetInstance();
, I get 'unresolved external symbol' error.
error LNK2019: unresolved external symbol "public: static class State * __cdecl State::GetInstance(void)" (?GetInstance@State@@SAPAV1@XZ) referenced in function "public: virtual void __thiscall MesherApp::setup(void)" (?setup@MesherApp@@UAEXXZ)
Also tried the following since some say "define outside of class" - what does that even mean?
class State
{
public:
...
}
static State* instance;
static State* GetInstance();
Looking at this question, Static method with a field I don't see how this applies. I am declaring in the .h and defining everything in the cpp file.