c++ - 对 `typeinfo for class' 的 undefined reference 和对 `vtable for class' 的 undefined reference

标签 c++ undefined-reference vtable

<分区>

我正在处理 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 元数据,并且如果该类声明了一个非纯、非内联虚函数,它将在与该函数定义相同的翻译单元中生成它。由于没有定义,因此不会生成它们,从而给出该错误。

关于c++ - 对 `typeinfo for class' 的 undefined reference 和对 `vtable for class' 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16460522/

相关文章:

c++ - 使用 clGetDeviceInfo 从内存读取访问冲突

javascript - 函数未定义: Must a javascript function be defined within the same file?

c++ - 我错过了什么?未定义引用

c++ - 什么是 undefined reference / Unresolved external symbol 错误,我该如何解决?

C# Hook vmtable

c++ - 在源代码中使用环境变量(编译时)

c++ - 当我尝试从嵌套类访问外部类的成员数据时,为什么会出现段错误?

c++ - 使用简单的 CRITICAL_SECTION,似乎陷入僵局

c# - vtables是如何在c++和c#中实现的?

c++ - 将 EXE 作为 DLL 加载,本地 vftable