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

Tar*_*nen 41 c++ constructor class

在编译以下头文件时,我得到了一个未定义的引用"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(){}
Run Code Online (Sandbox Code Playgroud)

我找不到这个有什么问题.

小智 77

您正在声明一个virtual函数而不是定义它:

virtual void calculateCredits();

定义它或将其声明为:

virtual void calculateCredits() = 0;

或者干脆:

virtual void calculateCredits() { };

阅读更多关于vftable的信息:http://en.wikipedia.org/wiki/Virtual_method_table

  • 在大多数ABI中,vtable在编译单元中发出,定义了未在类定义中定义的第一个虚函数.如果没有这样的话,它将全部发出.然后折叠多个这样的物体. (4认同)
  • 尽管定义了如上所示的虚函数,但我得到了相同的错误.还有什么我想念的吗? (3认同)