加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 03:36:13 所属栏目:百科 来源:网络整理
导读:我很确定这个问题是重复的,但我的代码在这里有所不同,以下是我的代码.它失败了“未定义的符号”错误,不确定丢失了什么. class Parent { public : virtual int func () = 0; virtual ~Parent(); }; class Child : public Parent { public : int data; Child (
我很确定这个问题是重复的,但我的代码在这里有所不同,以下是我的代码.它失败了“未定义的符号”错误,不确定丢失了什么.
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 10n";
        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;.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读