undefined reference to 'vtable for class'

2019-03-14 06:33发布

This question already has an answer here:

I am getting an undefined reference to `vtable for student' while compiling the following header file:

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

I can't find what is wrong in this.

1条回答
相关推荐>>
2楼-- · 2019-03-14 07:13

You're declaring a virtual function and not defining it:

virtual void calculateCredits();

Either define it or declare it as:

virtual void calculateCredits() = 0;

Or simply:

virtual void calculateCredits() { };

Read more about vftable: http://en.wikipedia.org/wiki/Virtual_method_table

查看更多
登录 后发表回答