c++ - Unresolved External Symbol in Singleton Class -
i've been coding long time not understand error. writing custom system providing unique integer id's specific instances of objects (i call them tags). , implementing 1 of classes singleton.
the 2 classes tagging system defined such:
#include "singleton.h"  class tag: public bear::singleton<tag> { public:     static duint32 requesttag(tagged* requester);     static void revoketags(void); private:     tag(void);     ~tag(void);      tagged** m_tagtable; // list of objects active tags     duint32  m_tagtable_capacity, // maximum capacity of tag table              m_tagindexer; // last given tag };  class tagged {     friend class tag; public:     inline duint32 gettag(void) {return m_tag;} private:     inline void invalidatetag(void) {m_tag=invalid_tag;}     duint32 m_tag; protected:     tagged();     virtual ~tagged(); }; the singleton class defined such:
template <typename t> class singleton { public:     singleton(void);     virtual ~singleton(void);      inline static t& getinstance(void) {return (*m_singletoninstance);} private:     // copy constructor not implemented on purpose     // prevents copying operations attempt copy yield     // compile time error     singleton(const singleton<t>& copyfrom); protected:     static t* m_singletoninstance; };  template <typename t> singleton<t>::singleton (void) {     assert(!m_singletoninstance);     m_singletoninstance=static_cast<t*>(this); }  template <typename t> singleton<t>::~singleton (void) {     if (m_singletoninstance)         m_singletoninstance= 0; } i getting following error upon trying compile , link files:
test.obj : error lnk2001: unresolved external symbol "protected: static class util::tag * bear::singleton::m_singletoninstance" (?m_singletoninstance@?$singleton@vtag@util@@@bear@@1pavtag@util@@a) 1>c:...\tools\debug\util.exe : fatal error lnk1120: 1 unresolved externals
does have idea why getting error?
you should provide definition static data member @ namespace scope (currently, have declaration):
template <typename t> class singleton {     // ...  protected:     static t* m_singletoninstance; // <== declaration };  template<typename t> t* singleton<t>::m_singletoninstance = nullptr; // <== definition if working c++03, can replace nullptr null or 0.
Comments
Post a Comment