【问题标题】:undefined reference to 'vtable for class' constructor [duplicate]未定义对'vtable for class'构造函数的引用[重复]
【发布时间】:2014-06-08 22:23:33
【问题描述】:

在编译以下头文件时,我得到了一个未定义的对“学生 vtable”的引用:

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

我找不到这里有什么问题。

【问题讨论】:

    标签: c++ class constructor


    【解决方案1】:

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

    virtual void calculateCredits();

    要么定义它,要么声明为:

    virtual void calculateCredits() = 0;

    或者简单地说:

    virtual void calculateCredits() { };

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

    【讨论】:

    • 谢谢,它成功了。实际上这只是头文件的一部分,我在下面有另一个类,它使用函数 calculateCredits() 我认为没有必要在学生类中定义。 .
    • 在大多数 ABI 中,vtable 是在定义类定义中未定义的第一个虚函数的编译单元中发出的。如果没有,它将全部发出。然后将折叠多个这样的对象。
    • 尽管定义了如上所示的虚函数,但我得到了同样的错误。我还缺少什么吗?
    • 很好的链接,非常感谢您在折腾了我 5 个小时后救了我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 2015-05-25
    • 2014-06-05
    • 2011-07-27
    相关资源
    最近更新 更多