为什么单例初始化失败(链接错误)

时间:2017-09-20 15:31:20

标签: c++ visual-studio singleton

我已经定义了一个单身如下:

struct Conditions {
    bool bMonolithic;
    bool bKeepVersion;
    bool bToc;

    static Conditions& initialize() {
        if (instance == nullptr)
            instance = new Conditions;
        return *instance;
    }
private: 
    Conditions():bMonolithic(false), bKeepVersion(false), bToc(false) {}
    static Conditions* instance;
};

当我尝试使用它时,我调用了initialize()函数:

Conditions& kcondition = Conditions::initialize();

然而,编译器给了我这个错误:

error LNK2001: unresolved external symbol "private: static struct Conditions * Conditions::instance" (?instance@Conditions@@0PEAU1@EA)

使用单身人士的正确方法是什么?

0 个答案:

没有答案