C++ 对“类的 vtable”构造函数的未定义引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23255256/
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
undefined reference to 'vtable for class' constructor
提问by Tarounen
I am getting an undefined reference to `vtable for student' while compiling the following header file:
在编译以下头文件时,我收到了对“vtable for student”的未定义引用:
student.h
学生.h
class student
{
private:
string names;
string address;
string type;
protected:
float marks;
int credits;
public:
student();
student(string n,string a,string t,float m);
~student();
string getNames();
string getAddress();
string getType();
float getMarks();
virtual void calculateCredits();
int getCredits();
};
student::student(){}
student::student(string n, string a,string t,float m)
{
names = n;
address = a;
marks = m;
}
student::~student(){}
I can't find what is wrong in this.
我找不到这有什么问题。
回答by
You're declaring a virtual
function and not defining it:
您正在声明一个virtual
函数而不是定义它:
virtual void calculateCredits();
virtual void calculateCredits();
Either define it or declare it as:
将其定义或声明为:
virtual void calculateCredits() = 0;
virtual void calculateCredits() = 0;
Or simply:
或者干脆:
virtual void calculateCredits() { };
virtual void calculateCredits() { };
Read more about vftable: http://en.wikipedia.org/wiki/Virtual_method_table
阅读有关 vftable 的更多信息:http: //en.wikipedia.org/wiki/Virtual_method_table