C++ 缺少 vtable 通常意味着第一个非内联虚拟成员函数没有定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31861803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A missing vtable usually means the first non-inline virtual member function has no definition
提问by Aparna Chaganti
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.
以下是编译时的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.
回答by Christian Hackl
Parent::~Parent()
is not defined.
Parent::~Parent()
没有定义。
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;
.
或者单独定义。或者,从 C++11 开始,编写virtual ~Parent() = default;
.
In any case, a destructor needs a definition.
在任何情况下,析构函数都需要定义。