0

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.

Jacob717
  • 23
  • 2
  • *"what code is required in the implementation (.cpp) file for the destructor."* None. It's already fully defined in the header. Do you get any errors when you actually compile the code? – Igor Tandetnik Mar 24 '19 at 00:07
  • @IgorTandetnik Yes, all of the errors in the original post were occurring when I tried to compile the code. That's what I'm confused about. Is it possible that the compiler is just "stuck" and spitting out errors that don't really exist? If so, how would I know/fix this? – Jacob717 Mar 24 '19 at 00:14
  • 1
    To the extent there is a problem, it must needs lie in the code you haven't shown. For further assistance, prepare a [mcve] – Igor Tandetnik Mar 24 '19 at 00:19
  • 1
    `virtual ~MyClass();` goes in the .h file, `MyClass::~MyClass(){};` goes in the .cpp file. – jackw11111 Mar 24 '19 at 01:27
  • 1
    Better: `virtual ~MyClass() = default;` – selbie Mar 24 '19 at 03:56
  • **"I have been provided with a header file that includes a base class declaration"**. Given that, is the expectation that you are to implement the methods in this base class (`MyClass`)? Or is it the expectation that you are to **derive your own class** from `MyClass` and have an inheritance relationship? That influences the correct answer greatly. – selbie Mar 24 '19 at 04:00
  • See [Undefined reference to vtable](https://stackoverflow.com/questions/3065154/undefined-reference-to-vtable). – 1201ProgramAlarm Mar 24 '19 at 04:29

0 Answers0