I'm working with classes and inheritance and I came across this first error in a program much bigger than the one I'm going to present to you:
undefined reference to vtable for class xxx
Trying to recreate the error in a small program I ran into this error
[Error] invalid new-expression of abstract class type 'bebe'
[Note] because the following virtual functions are pure within 'bebe':
[Note] virtual double abuelo::expresion()
And I think it points in the same direction. I know it has to do with the fact that the grandparent class is a pure virtual class, but I don't understand exactly what I should do, whether to stop making it pure, whether to implement the pure function in each function, whether to make the children virtual, whether do static cast, or if you just don't call the grandparent class. On the site in English I have found similar questions but the concepts in English are quite confusing to me, so I present the code:
#include <cctype>
#include <iostream>
#include <list>
#include <string>
using namespace std;
// clase abuelo es Virtual pura
class abuelo
{
public:
virtual double expresion() = 0;
};
// El resto de clases derivadas
class padre: public abuelo
{
public:
padre(abuelo *paterno, abuelo *materno);
static abuelo *foo();
private:
abuelo *paterno;
abuelo *materno;
};
abuelo *padre::foo()
{
return 0;
}
class madre: public abuelo
{
public:
static abuelo *foo();
};
class bebe: public madre
{
public:
bebe(string nombre)
{
this->nombre = nombre;
}
protected:
string nombre;
};
class hijo: public madre
{
public:
hijo(double value)
{
this->value = value;
}
double expresion()
{
return value;
}
private:
double value;
};
// LA funcion problematica
abuelo *madre::foo()
{
char paren;
double value;
cin >> ws;
if (isdigit(cin.peek()))
{
cin >> value;
//aquí quise declarar a hijo como new hijo pero me dijo que no
//reconocía a hijo
abuelo *hijo;// = new hijo(value);
return hijo;
}
if (cin.peek() == '(')
{
cin >> paren;
return padre::foo();
}
else
/* y aqui esta el error que les comento */
return new bebe("Luis");
return 0;
}
int main()
{
return 0;
}
I appreciate any light you can shed on this issue. Thanks.
Virtual functions are used to be able to customize the behavior of functions in derived classes when we are working in polymorphic environments:
Virtual functions have, as an additional feature, that they can be declared as pure virtual. This means that its initial declaration has no implementation. This feature is useful for implementing interfaces or in those situations where the function is needed in the base class but an implementation at that level does not make sense:
What happens then is that the class is considered as " abstract " or " incomplete ". C++ does not allow instantiating abstract classes. Declaring a pure virtual function forces you to implement the function in some derived class in order to create instances:
Your problem is precisely this, that you declare a pure virtual function and said function has no implementation in any of the classes you intend to create. The solution is to give it an implementation where it corresponds.
If you want to have an object of a class, that class will have to have an implementation of all its methods.
bebe
inherits (indirectly) fromabuelo
, so it has a method declaredexpresion()
. But said method, which you declare inabuelo
, you do not implement inabuelo
, nor inmadre
, nor inbebe
.So: what if someone does
bebe.expresion()
? There is no implementation of such a method. You have a class with unimplemented methods, so you cannot create any instances of that class.TL;DR: Implement
expresion()
.