Reputation: 107
I'm very new to C++ and I'm not quite sure how to override methods from base class. I apologize for not translating the code to English prior to posting, but I don't think it would make any difference.
A summary of what I'd like to do is create an abstract base class for linked lists, then derive singly/doubly/multiply linked/circular lists from it etc.
Here is the code:
#ifndef __LISTA_H__
#define __LISTA_H__
class Elem;
class JulElem;
class Lista // Abstract list class
{
public:
Lista();
~Lista();
virtual void dohvatiGlavu() = 0;
virtual void dodajNaKraj(int vred) = 0;
virtual void dodajIza(Elem* elem, int vr) = 0;
virtual void obrisiIza(Elem* elem) = 0;
virtual bool prazna() = 0;
virtual int velicina() = 0;
protected:
class Elem
{
protected:
int vred;
};
virtual Elem* noviElem();
private:
Elem* glava;
};
class Jul : Lista // Singly linked list
{
public:
void dohvatiGlavu();
void dodajNaKraj(int vred);
void dodajIza(Elem* elem, int vr);
void obrisiIza(Elem* elem);
bool prazna();
int velicina();
private:
class JulElem : Elem
{
};
JulElem* noviElem();
};
This is the error I get:
error C2555: 'Jul::noviElem': overriding virtual function return type differs and is not covariant from 'Lista::noviElem'
I've mostly seen people use structs for list elements but I'm not sure how that would fit my case scenario because different types of lists require different types of structs.
Thanks
Upvotes: 2
Views: 106
Reputation: 254471
The problem is that the inheritance is private, so JulElem*
is not covariant with Elem*
. Use public inheritance:
class JulElem : public Elem
^^^^^^
Upvotes: 3
Reputation: 6050
The return type of the function can not be overrided. you should do like this: in the base, define the function as:
virtual void noviElem(Elem** result);
The parameter "result" served as an output paramter
Upvotes: 1
Reputation: 1581
It seems like you need to use templates.
Here is an example :
template<class T>
T Add(T n1, T n2)
{
T result;
result = n1 + n2;
return result;
}
Upvotes: 0