Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.
like image 476
Aparna Chaganti Avatar asked Aug 06 '15 17:08

Aparna Chaganti


1 Answers

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.

like image 122
Christian Hackl Avatar answered Sep 17 '22 09:09

Christian Hackl