A missing vtable usually means the first non-inlin

2019-02-22 02:59发布

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.

1条回答
The star\"
2楼-- · 2019-02-22 03:26

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.

查看更多
登录 后发表回答