尝试使用单例时未解析的外部符号

时间:2018-11-03 02:45:54

标签: c++ oop singleton

我收到此错误:

“ LNK2001无法解析的外部符号“ private:静态类,示例*示例:: instance”(?instance @ Example @@ 0PAV1 @ A)”

当我运行以下代码时:

Example.h

class Example
{
private:

  static Example* instance;
  Example(){}

public:

  static Example* Instance();
};

Example.cpp

Example* Example::Instance()
{
    if (!instance)
    {
        instance = new Example;
    }

    return instance;
}

Entity.h

class Entity
{
private:

    Example* example;

public:

    Entity();
    Example* getExample();
};

Entity.cpp

Entity::Entity() : example(Example::Instance())
{
}

Example* Entity::getExample()
{
    return example;
}

我认为这与以下行有关:

Entity::Entity() : example(Example::Instance())

但是为什么这会引起问题?

0 个答案:

没有答案