单例继承链接器错误 [英] Singleton Inheritance Linker Error

查看:146
本文介绍了单例继承链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新用户,我收到此链接器错误

I'm new with C++, and I got this linker error,


LNK2001:未解析的外部符号private:static class DebugLog Singleton :: instance(?instance @?$ Singleton @ VDebugLog @@@@ 0VDebugLog @@ A)

LNK2001: unresolved external symbol "private: static class DebugLog Singleton::instance" (?instance@?$Singleton@VDebugLog@@@@0VDebugLog@@A)

有问题的代码:

template<typename T>
class Singleton {
public:
    static T& getInstance() {
        return instance;
    }
private:
    static T instance;
};

class DebugLog : public Singleton<DebugLog> {
public:
    void doNothing() {}
};

void main() {
    DebugLog::getInstance().doNothing();
}

任何人都可以告诉我如何解决链接器错误而不失去Singleton继承在DebugLog中?

Could anybody tell me how I can fix that linker error without losing the Singleton inheritance in DebugLog?

谢谢。

推荐答案

/ p>

You missed:

template<typename T>
T Singleton<T>::instance;

在类定义后插入这些行。

Insert those lines after your class definition.

为了初始化一个静态数据成员,我们必须在全局范围内的类之外包含一个正式的
定义。

In order to initialize a static data-member we must include a formal definition outside the class, in the global scope.

有关详情,请阅读此链接 (Section:Static members)

这篇关于单例继承链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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