小编use*_*665的帖子

C++ Singleton实现 - 静态问题

自从我用C++编程以来已经有一段时间了.我试图实现一个单例类,但我得到一个未解析的外部符号.你们能指出解决这个问题吗?提前致谢!

class Singleton
{
    Singleton(){}
    Singleton(const Singleton & o){}
    static Singleton * theInstance;

public:
    static Singleton getInstance()
    {
        if(!theInstance)
            Singleton::theInstance = new Singleton();

        return * theInstance;
    }
};
Run Code Online (Sandbox Code Playgroud)

错误:

错误3错误LNK1120:1个未解析的外部

错误2错误LNK2001:未解析的外部符号 "private: static class Singleton * Singleton::theInstance" (?theInstance@Singleton@@0PAV1@A)

c++ linker singleton static linker-errors

1
推荐指数
1
解决办法
5921
查看次数

标签 统计

c++ ×1

linker ×1

linker-errors ×1

singleton ×1

static ×1