Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the redefinition error for virtual destructor

Tags:

c++

I've the following C++ snippet:

#include <iostream>

using namespace std;

class A
{
public:
    virtual ~A()
    {
    }
};

A::~A()
{
    
}

int main(int argc, char * argv [])
{
    return 0;
}

Why am I getting these errors?:

error: redefinition of 'A::~A()' A::~A()

error: 'virtual A::~A()' previously defined here

 virtual ~A()**
like image 842
TryinHard Avatar asked Jan 18 '26 14:01

TryinHard


1 Answers

Simply use the following in your class

virtual ~A();

instead of

virtual ~A()
             {
              }

The compiler is actually telling you exactly what the problem is here. You have two implementations - one inline in your class and another outside it here

A::~A()
{

}

you cannot have both.

like image 164
mathematician1975 Avatar answered Jan 20 '26 07:01

mathematician1975



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!