This question already has answers here:
c++ undefined reference to vtable

(7个答案)


6年前关闭。




在编译以下头文件时,我得到对“vtable for student”的 undefined reference :

学生
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(){}

我找不到这有什么问题。

最佳答案

您在声明virtual函数,但未定义它:
virtual void calculateCredits();
定义它或将其声明为:
virtual void calculateCredits() = 0;
或者简单地:
virtual void calculateCredits() { };
阅读有关vftable的更多信息:http://en.wikipedia.org/wiki/Virtual_method_table

09-06 10:36
查看更多