C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 对类的’typeinfo’的未定义引用和对’class for class’的未定义引用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Undefined symbols “vtable for …” and “typeinfo for…”?5个
我正在处理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’的未定义引用和对’class for class’的未定义引用全部内容,希望文章能够帮你解决c – 对类的’typeinfo’的未定义引用和对’class for class’的未定义引用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。