c++ - 基类中的虚函数定义给出错误

标签 c++

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

这是我的代码:

#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*)'

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

最佳答案

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

析构函数的错误有点复杂,但基本上编译器需要决定在哪个对象文件中放置多态类的vtable - 它通常在第一个非- 定义纯非内联虚函数(如果没有此类函数,则具有更复杂的规则)。在这种情况下,您声明了两个外联虚拟函数,但从未定义它们,因此编译器永远不会将 vtable 写入对象文件。

关于c++ - 基类中的虚函数定义给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20612177/

相关文章:

c++ - 转换层次结构 - 将 parent 的方向应用于其 child

c++ - 如何使 MFC 中的按钮根据单击前一个按钮执行不同的操作?

c++ - 翻转 glReadPixels 的 RGBA 输出(C++ OpenGL)

c++ - 返回右值或左值

c++ - 解析 URL 字符串以删除不需要的内容 (C++)

c++ - 与 boost::condition_variable 的线程同步

c++ - 参数类型不兼容的 MPI 错误 "char"到 "const char"c++

c++ - 如何让一个类(class)成员保持未构造状态,以便以后使用 placement new 进行构造

c++ - std::unique_ptr 干扰某些 sf::RenderWindow 函数?

python - 当使用 -O 优化时,ctypes 返回错误值