I have been provided with a header file that includes a base class declaration. Within the class declaration is a virtual destructor that has been defined. From this, we need to code the implementation (.cpp) file. The code looks like this in the header file:
class MyClass
{
private: ...
protected: ...
public:
MyClass (double aBaseValue = 0.0);
virtual ~MyClass() { }
...
};
What I am struggling with is what code is required in the implementation (.cpp) file for the destructor. I thought that because I have defined it within the class declaration I do not need to define it again in the implementation file, but if I leave it out then I get the following error in the class declaration when hovering over my virtual destructor, as well on the constructor definition in my .cpp file.
undefined reference to `vtable for MyClass'
SO, if I instead add the following code to the source file MyClass::~MyClass(); like so
#include "MyClass.h"
MyClass::MyClass(double aBaseValue)
{
this->fBaseValue = aBaseValue;
{
MyClass::~MyClass();
then the first two errors go away, but now I'm given an error over the destructor code in the .cpp file
declaration of 'virtual MyClass::~MyClass()' outside of class is not definition [-fpermissive]
From what I can understand, the above error is telling me that I am basically trying to declare the destructor again as there is no braces... but if I put braces a get an error stating that there is a redefinition of the destructor.
I'm not sure what my other options are. Can someone please help me understand how I am supposed to be implementing a virtual destructor for both the class declaration and implementation? Thanks.