Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference to `typeinfo for class' and undefined reference to `vtable for class' [duplicate]

I'm dealing with inheritance in C++. I wanted to write a program for addition and subtraction of two arrays. Heres my code:

#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)
{
}

But I have a strange errors:

/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 ===|

dont know from where they came from, dont now how to 'fix' them. Thanks in advance;)

like image 945
Brian Brown Avatar asked May 09 '13 11:05

Brian Brown


People also ask

What does undefined reference to Vtable mean?

In summary, there are three key causes of the "undefined reference to vtable" error: A member function is missing its definition. An object file is not being linked. All virtual functions have inline definitions.17-Jun-2010.

How do you fix a undefined reference in C++?

How To Fix Undefined Reference in C++ You can fix undefined reference in C++ by investigating the linker error messages and then providing the missing definition for the given symbols. Note that not all linker errors are undefined references, and the same programmer error does not cause all undefined reference errors.

How do you fix undefined references to Main?

To fix this error, correct the spelling of the main() function.


3 Answers

root::setSize isn't declared pure virtual, which means it must be defined. Presumably, it should be as pure as the other functions:

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

If you're interested in the gory details of why you get that particular error: this compiler needs to generate the class's virtual/RTTI metadata somewhere and, if the class declares a non-pure, non-inline virtual function, it will generate it in the same translation unit as that function's definition. Since there is no definition, they don't get generated, giving that error.

like image 64
Mike Seymour Avatar answered Oct 18 '22 18:10

Mike Seymour


Your root::setSize is not defined, and not declared an pure virtual function. Either add = 0 to the end of the function (making it pure virtal), or define a root::setSize function.

like image 38
Mats Petersson Avatar answered Oct 18 '22 20:10

Mats Petersson


I believe it's because you haven't implemented

    virtual void setSize(int);

in root or declared it as pure virtual by adding =0

like image 1
Nick Avatar answered Oct 18 '22 18:10

Nick