Sign in to follow this  
EvilNando

Singleton problem

Recommended Posts

Im getting an weird error when creating a singleton class

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

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

SpriteBatch(); // private constructor

};
[/code]


on the source im just doing this
[code]

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

return instance;
}
[/code]


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


Ive made sure to include "SpriteBatch.h" at the source so Im out of ideas...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='Aardvajk' timestamp='1307726640' post='4821768']
Simple. I'll refrain from haranguing you on the design perils of singletons this once.
[/quote]
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.

Share this post


Link to post
Share on other sites
[quote name='EvilNando' timestamp='1307734900' post='4821835']
thanks[/quote]
You're welcome :)


[quote name='way2lazy2care' timestamp='1307735437' post='4821838']
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.[/quote]
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? :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this