A missing vtable usually means the first non-inline virtual member function has no definition

Aparna Chaganti picture Aparna Chaganti · Aug 6, 2015 · Viewed 28.2k times · Source

I am pretty sure this question is duplicate, but my code is different here, the following is my code. It fails with a "Undefined symbols" error, not sure whats missing.

class Parent {
   public :
     virtual int func () = 0;
     virtual ~Parent();

 };


 class Child : public Parent {
     public :

     int data;
     Child (int k) {
        data = k;
      }
    int func() {   // virtual function
       cout<<"Returning square of 10\n";
        return 10*10;
    }

    void Display () {
    cout<<data<<"\n";

 }

 ~ Child() {

    cout<<"Overridden Parents Destructor \n";

 }
};



int main() {
  Child a(10);
 a.Display();

 }

The following is the O/P when compiled.

Undefined symbols for architecture x86_64:
  "Parent::~Parent()", referenced from:
      Child::~Child() in inher-4b1311.o
  "typeinfo for Parent", referenced from:
      typeinfo for Child in inher-4b1311.o
  "vtable for Parent", referenced from:
      Parent::Parent() in inher-4b1311.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.

Answer

Christian Hackl picture Christian Hackl · Aug 6, 2015

Parent::~Parent() is not defined.

You can put the definition directly into the class definition:

class Parent {
   public :
     virtual int func () = 0;
     virtual ~Parent() {};
 };

Or define it separately. Or, since C++11, write virtual ~Parent() = default;.

In any case, a destructor needs a definition.