微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 缺少vtable通常意味着第一个非内联虚拟成员函数没有定义

我很确定这个问题是重复的,但我的代码在这里有所不同,以下是我的代码.它失败了“未定义的符号”错误,不确定丢失了什么.
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();

 }

以下是编译时的O / P.

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.

解决方法

未定义Parent ::〜Parent().

您可以将定义直接放入类定义中:

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

或者单独定义它.或者,从C 11开始,写入virtual~Parent()= default;.

无论如何,析构函数需要一个定义.

原文地址:https://www.jb51.cc/c/110902.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐