基类中的虚拟函数定义出现错误

Virtual function definition in base class gives error

本文关键字:错误 定义 函数 虚拟 基类      更新时间:2023-10-16

我已经编写了一组类来检查组合模式。

这是我的代码:

#include <iostream>
#include <string>
#include <list>
#include "InvalidCompositeException.h"
using namespace std;
class Composite;
class Component {
public:
    Component() {}
    virtual ~Component() {}
    virtual string getName() = 0;
    virtual int getNetPrice() = 0;
    virtual Composite* getComposite() {
        try {
            throw myEx;
        } catch (InvalidCompositeException& e) {
            cout<<"Exception: "<<e.what();
        }
        return 0;
    }
    **virtual void add(Component* c);
    virtual void remove(Component* c);**
private:
};
class Composite : public Component {
public:
    Composite():mChildList(new list<Component*>()) {}
    virtual ~Composite() {
        mChildList->clear();
        delete mChildList;
    }
    virtual string getName() {return "Composite";}
    virtual int getNetPrice() {
        list<Component*>::iterator i;
        int sum = 0;
        for(i=mChildList->begin(); i!= mChildList->end(); ++i) {
            sum = sum + (*i)->getNetPrice();
        }
        return sum;
    }
    virtual void add(Component* c) {
        mChildList->push_back(c);
    }
    virtual void remove(Component* c) {
        mChildList->remove(c);
    }
private:
    list<Component*>* mChildList;
};
class Container: public Composite {
public:
    Container() {}
    virtual ~Container() {}
    string getName() {
        cout<<"container"<<endl;
        return "container";
    }
};
class Line: public Component {
public:
    Line(): mNetPrice(50) {}
    ~Line() {};
    int getNetPrice() { return mNetPrice; }
    string getName() {
        cout<<"line"<<endl;
        return "line";
    }
private:
    int mNetPrice;
};
class Text: public Component {
public:
    Text(): mNetPrice(100) {}
    ~Text() {};
    int getNetPrice() { return mNetPrice; }
    string getName() {
        cout<<"Text"<<endl;
        return "Text";
    }
private:
    int mNetPrice;
};
int main(void) {
    Container* c = new Container();
    Line* l = new Line();
    Text* t = new Text();
    c->add(l);
    c->add(l);
    c->add(t);
    cout<<"total price for c is "<<c->getNetPrice();
    l->getComposite();
    delete t;
    delete l;
    delete c;
    return EXIT_SUCCESS;
}

我的代码运行良好,除非当我在父类中添加那些粗体行时,我会收到错误

undefined reference to `vtable for Component'   // on this line virtual ~Component() {}
undefined reference to `Component::add(Component*)'
undefined reference to `Component::remove(Component*)'

我没有将虚拟函数定义为纯函数。那么,为什么我会收到这些错误,即使我没有在行和文本类中定义它们。如果我不添加那些大胆的声明,我的代码就可以正常工作。其次,为什么析构函数出错?

非纯虚拟函数需要有一个定义,即使它们从未被调用(这样链接器就可以在vtable中放入一些东西)。只需添加=0即可使类抽象化,或者提供空的定义。

析构函数的错误有点复杂,但基本上编译器需要决定将多语言类的vtable放在哪个对象文件中——它通常在定义第一个非纯、非内联虚拟函数的地方这样做(在没有此类函数的地方有更复杂的规则)。在这种情况下,您声明了两个离线的虚拟函数,但从未定义它们,因此编译器从未将vtable写入对象文件。

如果基类中没有实现,则需要使用虚拟函数声明中的=0使它们抽象。否则,基类的虚拟函数表将尝试查找实体-如果没有=0,则表示它们必须存在,并且最终将不指向任何对象,从而导致链接器错误。

析构函数错误也是一样的——它需要完整的表来找到虚拟dtor,而表并不完整。

考虑:

class foo
{
 public:
    void doit();
 };
foo f;
 f.doit(); // linker error, doit isn't defined!

如果void doit()变成了virtual doit((),你认为会发生什么?没什么,和你们现在犯的错误一样。然而,虚拟void doit()=0通过使其为纯将解决该错误。虚拟函数和其他函数一样,必须有一个实现。