0

Something that I learned from this question, but that I thought I'd give a more detailed and easily searchable answer to, is that if you have a base abstract class meant to be inherited such as this, and you've made its destructor virtual pure by adding "= 0" to its end:

class Base {
public:
    virtual ~Base() = 0;
};

and you try to derive a class with it, like this:

class Derived : public Base {
public:
    ~Derived();
};

Derived::~Derived() {
    // nothing, this is an example
}

you will get linker errors that are complaining about a lack of implementation for the base's destructor, despite it being a pure virtual method (on MSVC you might get LNK2019 and LNK 2001 errors). So how do you solve this problem?

ProgramGamer
  • 402
  • 5
  • 12
  • Possible duplicate of [Pure virtual destructor in C++](https://stackoverflow.com/questions/630950/pure-virtual-destructor-in-c) – Mike Kinghan Feb 09 '18 at 22:50

1 Answers1

0

The answer is that you need to provide an implementation of the base's destructor, despite it being a pure virtual function. The reasoning can be found in the question I linked above.

Knowing this, we can make changes to the base class, and this will work!

class Base {
public:
    virtual ~Base() = 0;
};

// This was added to get rid of the error
Base::~Base() {
    // nothing, although you can add your own code if necessary
}

class Derived : public Base {
public:
    ~Derived();
};

Derived::~Derived() {
    // nothing, this is an example
}
ProgramGamer
  • 402
  • 5
  • 12