gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:42:12 24 4
gpt4 key购买 nike

我很确定这个问题是重复的,但我的代码在这里不同,以下是我的代码。它因“ undefined symbol ”错误而失败,不确定缺少什么。

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;

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

关于c++ - 缺少 vtable 通常意味着第一个非内联虚拟成员函数没有定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53690663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com