2013-12-16 28 views
1

我写了一组类来检查组合模式。基类中的虚函数定义给出了错误

这里是我的代码:

#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 Classes中定义它们,我为什么会收到这些错误。如果我不添加这些大胆的声明我的代码工作正常。其次为什么析构函数的错误?

回答

2

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

与析构函数有关的错误有一点涉及,但基本上编译器需要决定在哪个对象文件中为您的polymophic类放置vtable--它通常在第一个非纯的非内联虚函数被定义(在没有这种函数的情况下,规则更复杂)。在这种情况下,你声明了两个out-of-line虚拟函数,但是从来没有定义它们,所以编译器不会将vtable写入一个目标文件。

2

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

析构函数错误是同样的事情 - 它需要完整的表来找到虚拟的dtor和表是不完整的。

0

考虑:

class foo 
{ 
public: 
    void doit(); 
}; 

foo f; 
f.doit(); // linker error, doit isn't defined! 

你觉得会发生什么,如果无效DOIT()成为虚拟DOIT()?没有什么,你现在有同样的错误。然而,通过使它为纯虚拟虚函数doit()= 0将解决错误。虚拟函数与其他任何函数一样,它必须有一个实现。

+0

嗯,这里的重点是,如果一个非虚函数永远不会被调用,那么您可以逃避不提供定义。在这里,基类方法永远不会被调用(两个子类都会覆盖它们),但定义仍然是必需的 - 所以它实际上与非虚拟案例完全不同。 –

相关问题