D
D
Danil Vaskevich2021-08-28 12:46:14
C++ / C#
Danil Vaskevich, 2021-08-28 12:46:14

How to solve LNK2001 error?

Hello, I work with abstract factories and set the task to remake it under Singleton. It seems that everything was done correctly, but the compiler produces: Error LNK2001 unresolved external symbol "private: static class WinterFactory * WinterFactory::instance" ([email protected]@@[email protected]).
The code:

__interface IJacket
{
  void wearOn();
  string color();
};

class WinterJacket : public IJacket {
public:
  void wearOn() {
    cout << "This jacket is warm\n";
  }

  string color() {
    return "grey";
  }
};

class AutumnJacket : public IJacket {
public:
  void wearOn() {
    cout << "This jacket is comfortable\n";
  }

  string color() {
    return "Yellow";
  }
};

__interface IShoes {
  void wearOn();
  bool isSport();
};

class Sneakers : public IShoes {
public:
  void wearOn() {
    cout << "This sneakers are comfortable\n";
  }

  bool isSport() {
    return true;
  }
};

class Boots : public IShoes {
public:
  void wearOn() {
    cout << "This boots are warm\n";
  }

  bool isSport() {
    return false;
  }
};
__interface IFactory
{
  IJacket* createJacket();
  IShoes* createShoes();
};

class WinterFactory : public IFactory {
private:
  static WinterFactory* instance;
  WinterFactory() = default;
  WinterFactory(const WinterFactory&) = delete;
public:
  static WinterFactory* getInstance() {
    if (instance == nullptr)
    {
      instance = new WinterFactory();
    }
    return instance;
  }
  IJacket* createJacket() { 
    return new WinterJacket(); 
  }
  IShoes* createShoes() {
    return new Boots();
  }
};


int main()
{
  WinterFactory *WF = WinterFactory::getInstance();
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
cunion, 2021-08-28
@DaniVffgf

Under the factory class, add It wouldn't hurt to format the code and replace it with . Also, the use of pointers does not make sense (especially raw ones).
WinterFactory* WinterFactory::instance = nullptr;
__interfaceclass

A
Adamos, 2021-08-28
@Adamos

Why should instance be a member of a class? Instead
static WinterFactory* instance;
Insert the first line in getInstance
static WinterFactory* instance = nullptr;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question