未定义引用'vtable for class'构造函数

时间:2014-04-23 21:11:53

标签: c++ class constructor

在编译以下头文件时,我得到了对'vtable for student'的未定义引用:

student.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(){}

我找不到这个错误。

1 个答案:

答案 0 :(得分:71)

您正在声明virtual功能而不是定义它:

virtual void calculateCredits();

定义它或将其声明为:

virtual void calculateCredits() = 0;

或者简单地说:

virtual void calculateCredits() { };

详细了解vftable:http://en.wikipedia.org/wiki/Virtual_method_table