Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Singleton - unresolved external?

This topic is 5307 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

hmm, to me it seems like I have done an ok Singleton class, but It seems I get an external error when I build the application :/ Singleton.h

template <class T>;
class Singleton {
	static T& GetInstance();
	Singleton(const Singleton&);
	Singleton& operator = (const Singleton&);

	static T& m_instance;

template <class T>
T* Singleton<T>::m_instance = 0;

#include "Singleton.h"

template <class T>
T& Singleton<T>::GetInstance() {
	if (m_instance == 0) {
		m_instance = new T;
	return *m_instance;

then I have a class MyClass and try to make it a singleton, like this:
typedef Singleton<MyClass>; MyClass;
MyClass& g_MyClass = MyClass::GetInstance(); 
But the "MyClass& g_MyClass = MyClass::GetInstance();" gives me an unresolved external when linking, and I can't figure out what is wrong :/ Would appreicate some help on this [edited by - bilsa on January 5, 2004 2:09:00 PM]

Share this post

Link to post
Share on other sites
Its the old class template function definition in a seperate .cpp file problem.

Theres 3 solutions i know of,

1) Define GetInstance inside the the header file.
2) #include the .cpp file at the bottom of the header file
3) Use the export keyword (which isnt very well supported by compilers)

Another problem seems to be m_instance, you are treating it as a pointer when you have declared it as static T& m_instance;

Share this post

Link to post
Share on other sites
hmm, now that I recall it this was the problem that I had once when I used VC++ 6.0

But then I got a hand on VC++ .NET 2003 and I was told that this compiler was enhanced in templates, and that you could have the templated functions in a .cpp without any problem what so ever. So I haven''t thought that it could be the problem... but now that you mention it :/

Does anyone know anything about VC++ .NET 2003 and how it supports templates?


Share this post

Link to post
Share on other sites
It wouldn''t make any sense to compile every possible version of a template class, even ones of types that the .cpp file doesn''t know exists, before linking stage. That is why templates have to be visible to the code that uses it.

Share this post

Link to post
Share on other sites
typedef Singleton; MyClass; ??????
typedef Singleton MyClass;


"You wished for a white christmas... Now go shovel your wishes!"

Share this post

Link to post
Share on other sites

  • 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!