基类中的虚拟函数定义出现错误
Virtual function definition in base class gives error
我已经编写了一组类来检查组合模式。
这是我的代码:
#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通过使其为纯将解决该错误。虚拟函数和其他函数一样,必须有一个实现。
相关文章:
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 使用命名空间时出现多个定义错误
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编写代码时C++出现错误:错误 1 错误 C2601:'circle':本地函数定义是非法的
- 在运算符重载定义中使用成员函数(const错误)
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在C++中使用内联方法时出现未定义的符号错误
- 已定义函数时出现 G++ "未定义的引用"错误
- C++ G++ 编译器 - 错误:隐式声明的定义
- 链接 cmake 时出现未定义的引用错误
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 错误:未定义对'oboe::AudioStreamBuilder::openStream(oboe::AudioStream**)'的引用
- 链接阶段出现多重定义错误
- 已经以性格错误定义了
- vim ctag导致了对乐趣的错误定义
- C++ 隐式声明的编译器错误定义
- 编译器错误-定义问题.C++