对"类的typeinfo"的未定义引用和对"vtable for class"的未定义引用
Undefined reference to `typeinfo for class' and undefined reference to `vtable for class'
我正在处理c++中的继承。我想写一个程序来做两个数组的加减运算。下面是我的代码:
#include <iostream>
#include <cmath>
#include <sstream>
using namespace std;
class root
{
protected :
int size;
double *array;
public :
virtual ~root() {}
virtual root* add(const root&) = 0;
virtual root* sub(const root&) = 0;
virtual istream& in(istream&, root&) = 0;
virtual int getSize() const = 0;
virtual void setSize(int);
virtual int getAt(int) const = 0;
};
class aa: public root
{
public :
aa();
aa(int);
aa(const aa&);
root* add(const root& a);
root* sub(const root& a);
istream& in(istream&, root&){}
int getSize() const;
void setSize(int);
int getAt(int) const;
};
class bb: public root
{
public:
bb() { }
bb(const bb& b) { }
root* add(const root& a);
root* sub(const root& a);
istream& in(istream&, root&){}
int getSize() const{}
void setSize(int){}
int getAt(int) const{}
};
aa::aa()
{
size = 0;
array = NULL;
}
aa::aa(int nsize)
{
size = nsize;
array = new double[size+1];
for(int i=0; i<size; i++)
array[i] = 0;
}
root* aa::add(const root& a)
{
for (int i=0; i<a.getSize(); i++)
array[i] += a.getAt(i);
return new aa();
}
root* aa::sub(const root& a)
{
}
int aa::getSize() const
{
return size;
}
void aa::setSize(int nsize)
{
size = nsize;
array = new double[size+1];
for(int i=0; i<size; i++)
array[i] = 0;
}
int aa::getAt(int index) const
{
return array[index];
}
root* bb::add(const root& a)
{
return new bb();
}
root* bb::sub(const root& a)
{
}
int main(int argc, char **argv)
{
}
但是我有一个奇怪的错误:
/home/brian/Desktop/Temp/Untitled2.o||In function `root::~root()':|
Untitled2.cpp:(.text._ZN4rootD2Ev[_ZN4rootD5Ev]+0xb)||undefined reference to `vtable for root'|
/home/brian/Desktop/Temp/Untitled2.o||In function `root::root()':|
Untitled2.cpp:(.text._ZN4rootC2Ev[_ZN4rootC5Ev]+0x8)||undefined reference to `vtable for root'|
/home/brian/Desktop/Temp/Untitled2.o:(.rodata._ZTI2bb[typeinfo for bb]+0x8)||undefined reference to `typeinfo for root'|
/home/brian/Desktop/Temp/Untitled2.o:(.rodata._ZTI2aa[typeinfo for aa]+0x8)||undefined reference to `typeinfo for root'|
||=== Build finished: 4 errors, 0 warnings ===|
不知道他们从哪里来,不知道如何"修复"他们。提前感谢;)
root::setSize
没有被声明为纯虚的,这意味着它必须被定义。据推测,它应该像其他函数一样纯粹:
virtual void setSize(int) = 0;
^^^
如果你对为什么会得到这个特定错误的细节感兴趣:这个编译器需要在某个地方生成类的虚/RTTI元数据,如果类声明了一个非纯的、非内联的虚函数,它将在与该函数定义相同的翻译单元中生成它。由于没有定义,它们不会被生成,从而产生该错误。
您的root::setSize
没有定义,也没有声明为纯虚函数。或者将= 0
添加到函数的末尾(使其成为纯虚函数),或者定义root::setSize
函数。
我相信这是因为你还没有实现
virtual void setSize(int);
在root
中或者通过添加=0
相关文章:
- 编译C++时未定义的引用
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 未定义的引用在哪里
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 对 Scalar ::Scalar() 的未定义引用
- 已定义函数时出现 G++ "未定义的引用"错误
- 链接 cmake 时出现未定义的引用错误
- 对复制 CTOR 和 CTOR 的未定义引用
- 错误:未定义对'oboe::AudioStreamBuilder::openStream(oboe::AudioStream**)'的引用
- 右值引用未定义的行为
- 对"vtable for DigitalClock"的未定义引用 - 未定义对"DigitalClock::staticMetaObject"的引用 - Qt
- 函数类型到函数类型的引用:未定义的引用
- avcodec_alloc_context引用未定义,但ffmpeg链接顺序正确
- 从c++调用Fortran子程序,链接时引用未定义
- 枚举引用未定义,但位于包含的头文件中