0

I am getting this error:

"LNK2001 unresolved external symbol "private: static class Example * Example::instance" (?instance@Example@@0PAV1@A)"

when I run the following code:

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

I think it has something to do with this line:

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

But why does this cause a problem?

  • 2
    No it has to do with `static Example* instance;`. That's only a *declaration*, and you never *define* the `instance` variable anywhere. There's quite a few duplicates around here and on the Internet in general. – Some programmer dude Nov 03 '18 at 02:51
  • 1
    The pattern you are using is obsolete with C++11. `class singleton { private: singleton(); // Disallow instantiation outside of the class. singleton(singleton const &) = delete; operator=(singleton const &) = delete; public: static auto instance() { static singleton s; return &s; } };` – Swordfish Nov 03 '18 at 03:07
  • Thanks guys. I was able to fix my problem using your tips. – CheetahBongos Nov 03 '18 at 03:56

0 Answers0