Singleton problem

Started by
3 comments, last by Aardvajk 12 years, 10 months ago
Im getting an weird error when creating a singleton class


class SpriteBatch
{
public:
~SpriteBatch(); // public destructor
static SpriteBatch* GetInstance(void); // retunrs singleton instance

private:
static SpriteBatch* instance; // singleton instance pointer

SpriteBatch(); // private constructor

};



on the source im just doing this


SpriteBatch* SpriteBatch::GetInstance()
{
if (!instance)
{
instance = new SpriteBatch;
}

return instance;
}



Im getting the following error
Error 1 error LNK2001: unresolved external symbol "private: static class SpriteBatch * SpriteBatch::instance" (?instance@SpriteBatch@@0PAV1@A) E:\Opengl Game\Engine\SpriteBatch.obj Engine



Ive made sure to include "SpriteBatch.h" at the source so Im out of ideas...
Advertisement
In C++, when you declare a static member, you also have to define it somewhere.

So, in the .h you'd have the declaration as you have. In SpriteBatch.cpp, you'd then need:

[source lang="cpp"]
SpriteBatch *SpriteBatch::instance;
[/source]

Simple. I'll refrain from haranguing you on the design perils of singletons this once.
thanks

Simple. I'll refrain from haranguing you on the design perils of singletons this once.

BOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO.

edit:
I've been debugging something all this week that ended up being a singleton's state being changed in multiple places to things it shouldn't be so I may be slightly more bitter than normal about it and have no energy left to argue about it.

thanks

You're welcome :)



I've been debugging something all this week that ended up being a singleton's state being changed in multiple places to things it shouldn't be so I may be slightly more bitter than normal about it and have no energy left to argue about it.

Well, I really can't be bothered to turn another "Question relating to a singleton" thread into a "The evils of singletons" thread. I think we have quite enough of those already. Let's just treat this as a "Question about static members" thread, eh? :)

This topic is closed to new replies.

Advertisement