0

I think something is wrong with the deconstructor but have no idea. In fact, I don't understand the usage of deconstructor. The code is like this

#include <iostream>
using namespace std;
class A
{
public:
explicit A(int x);
virtual ~A()=default;
protected:
int a;
void function1(int X);
};

class B:public A
{
public:
explicit B(int x);
~B();
private:
int c;
};

void A::function1(int X){
  std::cout << "function1 " << X<< endl;
}

A::A(int x):a(0)
{
std::cout << "A " << x+a<< endl;
}

B::B(int x):A(x),c(3)
{
  std::cout << "B " << x+c<< endl;
}

int main()
{
B b1(1);
return 0;
}

It shows

/home/qiuyilin/projects/inheritance/main.cpp:36: undefined reference to vtable for B' CMakeFiles/inheritance.dir/main.cpp.o: In functionmain': /home/qiuyilin/projects/inheritance/main.cpp:43: undefined reference to `B::~B()' collect2: error: ld returned 1 exit status

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Qiuren
  • 19
  • 3

1 Answers1

0

As the error text says

undefined reference to `B::~B()

you did not define the destructor ~B(). It is only declared in the class definition of B but not defined.

You could write for example

~B() = default;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335