单例类中未解析的外部符号 [英] Unresolved External Symbol in Singleton Class

查看:126
本文介绍了单例类中未解析的外部符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编码了很长时间,但我不明白这个错误。我编写一个自定义系统,为对象的特定实例提供唯一的整数ID(我称之为标签)。

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 ids to specific instances of objects (I call them tags). And I am implementing one of the classes as a Singleton.

Tagging系统的两个类被定义为:

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();
};

Singleton类的定义如下:

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:

1> test.obj:error LNK2001:未解析的外部符号protected:static class util :: Tag * bear :: Singleton :: m_SingletonInstance(?m_SingletonInstance @? $ singleton @ VTag @ util @@@ bear @@ 1PAVTag @ util @@ A)
1> C:... \tools\Debug\util.exe:致命错误LNK1120:1 unresolved externals

1>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?

推荐答案

您应该在命名空间范围为您的静态数据成员提供一个定义(目前您只有一个声明):

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



如果使用C ++ 03, code> nullptr 与 NULL 0

这篇关于单例类中未解析的外部符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆