1

I've been coding for a long time now but I do not understand this error. I am writing a custom system for providing unique integer ID's to specific instances of objects (I call them tags). And I am implementing one of the classes as a Singleton.

The two classes for the Tagging system are defined as 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 all objects with active tags
    dUINT32  m_tagTable_capacity, // the maximum capacity of the 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 is defined as 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
    // this prevents copying operations any attempt to copy will yield
    // a 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 am getting the following error upon trying to compile and link together the 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 anyone have any idea why I am getting this error?

valiano
  • 16,433
  • 7
  • 64
  • 79
teddy
  • 171
  • 1
  • 1
  • 12
  • Not related directly to your problem, but I'd suggest looking at Scott Meyers' singleton pattern: http://stackoverflow.com/questions/1661529/is-meyers-implementation-of-singleton-pattern-thread-safe – Bret Kuhns May 15 '13 at 00:42

1 Answers1

2

You should provide a definition for your static data member at namespace scope (currently, you only have a declaration):

template <typename T>
class Singleton
{
    // ...

protected:
    static T* m_SingletonInstance; // <== DECLARATION
};

template<typename T>
T* Singleton<T>::m_SingletonInstance = nullptr; // <== DEFINITION

If you are working with C++03, you can replace nullptr with NULL or 0.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • thank you so much. this is one of the first times oddly enough that I have done serious coding with templates and I was completely unaware of that. – teddy May 15 '13 at 00:42
  • Just a question further though. Do I have to declare every static variable in a class or is this simply a template specific nuance? – teddy May 15 '13 at 00:46
  • @bear: You're welcome :-) If you need "define", then there are a few exceptions admitted, but mostly yes, you have to provide a definition at namespace scope – Andy Prowl May 15 '13 at 00:50