【问题标题】:Undefined reference to `typeinfo for class' and undefined reference to `vtable for class' [duplicate]对“类的类型信息”的未定义引用和对“类的vtable”的未定义引用[重复]
【发布时间】:2013-05-03 20:31:10
【问题描述】:

我正在处理 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 ===|

不知道它们来自哪里,现在不知道如何“修复”它们。提前致谢;)

【问题讨论】:

  • 不要使用裸指针。只是不要去那里。这是非常糟糕的代码。 (另外,查看构造函数初始化列表是如何工作的。)

标签: c++ undefined-reference vtable


【解决方案1】:

root::setSize 没有被声明为纯虚拟,这意味着它必须被定义。想必它应该和其他函数一样纯粹:

virtual void setSize(int) = 0;
                          ^^^

如果您对为什么会出现该特定错误的血腥细节感兴趣:此编译器需要在某处生成类的虚拟/RTTI 元数据,并且如果该类声明了一个非纯、非内联的虚函数,它将在与该函数定义相同的翻译单元中生成它。由于没有定义,因此不会生成它们,从而给出错误。

【讨论】:

  • +1 提供血淋淋的细节!
【解决方案2】:

我相信是因为你没有实现

    virtual void setSize(int);

root 中或通过添加=0 将其声明为纯虚拟

【讨论】:

    【解决方案3】:

    您的root::setSize 未定义,也未声明为纯虚函数。要么将= 0 添加到函数的末尾(使其成为纯虚函数),要么定义一个root::setSize 函数。

    【讨论】:

      猜你喜欢
      • 2015-09-24
      • 2012-02-15
      • 1970-01-01
      • 2011-11-12
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 2019-11-02
      • 2014-09-24
      相关资源
      最近更新 更多