Jump to content
  • Advertisement
Sign in to follow this  
EvilNando

Singleton problem

This topic is 2592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...

Share this post


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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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? :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!