public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* linking error with static templated member
@ 2008-01-28 21:53 Akos Rajna
  2008-01-29  4:05 ` Ian Lance Taylor
  0 siblings, 1 reply; 2+ messages in thread
From: Akos Rajna @ 2008-01-28 21:53 UTC (permalink / raw)
  To: gcc-help

Hi,

i'v tried to search for a solution everywhere without a luck so i hope
you guys can help. i have the following singleton template and it
works fine with vc++ however not with gcc.

template<class O> class SingletonInstance {
	O* inst;

public:
	SingletonInstance() {
		inst = NULL;
	}

	~SingletonInstance() {
		finalize();
	}

	O* instance() {
		if (inst == NULL)
			inst = new O();

		return inst;					
	}

	void finalize() {
		if (inst != NULL)
			delete inst;

		inst = NULL;
	}
};

template<class O> class Singleton {
public:
	static SingletonInstance<O> singleton;

public:
	Singleton() {
	}

	static O* instance() {
		return singleton.instance();
	}

	static void finalize() {
		singleton.finalize();
	}
};

class NetworkManager : public Singleton<NetworkManager>
{
private:
	NetworkManager();
	~NetworkManager();

	friend class SingletonInstance<NetworkManager>;

};

-----------------

template<> SingletonInstance<NetworkManager>
Singleton<NetworkManager>::singleton;

-----------------

and the linker is saying that Singleton<NetworkManager>::singleton is undefined

thanks in advance

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: linking error with static templated member
  2008-01-28 21:53 linking error with static templated member Akos Rajna
@ 2008-01-29  4:05 ` Ian Lance Taylor
  0 siblings, 0 replies; 2+ messages in thread
From: Ian Lance Taylor @ 2008-01-29  4:05 UTC (permalink / raw)
  To: Akos Rajna; +Cc: gcc-help

"Akos Rajna" <akimaki23@gmail.com> writes:

> and the linker is saying that Singleton<NetworkManager>::singleton is undefined

The C++ language requires a definition of the static class variable,
to go with the declaration.  I didn't see that in your code snippet.

template<class O> SingletonInstance<O> singleton;

Ian

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-01-28 19:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-28 21:53 linking error with static templated member Akos Rajna
2008-01-29  4:05 ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).