I'm trying to build a custom list implementation (for exercise purpose). Currently I have created an implementation with cursors and the usual one with pointers (circular doubly-linked list). I don't want to create different generic functions (print list, natural merge sort, etc.) in which the only difference is the class used). So I created an abstract class with pure virtual methods to use it as an interface. I used conditional compilation to deal with the different implementation of the position type (an int in the cursor implementation and a pointer to a node in the circular doubly-linked list). However I have these errors:
overriding 'Cella<T>* Lista<T>::primoLista() const [with T = char; Lista<T>::posizione = Cella<char>*]'
overriding 'Cella<T>* Lista<T>::primoLista() const [with T = char*; Lista<T>::posizione = Cella<char*>*]'
overriding 'Cella<T>* Lista<T>::primoLista() const [with T = float; Lista<T>::posizione = Cella<float>*]'
overriding 'Cella<T>* Lista<T>::primoLista() const [with T = int; Lista<T>::posizione = Cella<int>*]'
I'll write the main files.
Lista.h
#ifdef USE_CURSOR
#include "Cella.h"
#else
#include "cellalp.h"
#endif
template <class T> class Lista
{
public:
#ifdef USE_CURSOR
typedef int posizione;
#else
typedef Cella<T>* posizione;
#endif
virtual void insLista(T elem, posizione p) = 0;
virtual void cancLista(posizione p) = 0;
virtual bool listaVuota() const = 0;
virtual T leggiLista(posizione p) const = 0;
virtual void scriviLista(T elem, posizione p) = 0;
virtual posizione primoLista() const = 0;
virtual bool fineLista(posizione p) const = 0;
virtual posizione succLista(posizione p) const = 0;
//virtual posizione predLista(posizione p) const = 0;
};
cellalp.h
template <class T> class Cella
{
public:
typedef T tipoelem;
Cella();
Cella(tipoelem);
void setElemento(tipoelem);
tipoelem getElemento() const;
void setSucc(Cella*);
Cella* getSucc() const;
void setPrec(Cella*);
Cella* getPrec() const;
bool operator ==(Cella);
private:
tipoelem elemento;
Cella* prec;
Cella* succ;
};
// Implementazione della classe CellaLista
// --------------------------------------
// costruttori
template <class T> Cella<T>::Cella()
{}
template <class T> Cella<T>::Cella(tipoelem e)
{
elemento = e;
}
template <class T> void Cella<T>::setElemento(tipoelem label)
{
elemento = label;
}
template <class T> T Cella<T>::getElemento() const
{
return elemento;
}
template <class T> void Cella<T>::setSucc(Cella<T>* p)
{
succ=p;
}
template <class T> Cella<T>* Cella<T>::getSucc() const
{
return succ;
}
template <class T> void Cella<T>::setPrec(Cella<T>* p)
{
prec=p;
}
template <class T> Cella<T>* Cella<T>::getPrec() const
{
return prec;
}
// sovraccarico dell'operatore ==
template <class T> bool Cella<T>::operator==(Cella<T> cella)
{
return (getElemento == cella.getElemento);
}
listap.h
#include "Lista.h"
#include <iostream>
using namespace std;
template<class T>
class circLista : public Lista<T>
{
public:
circLista();
~circLista();
// circLista(const circLista<T>&);
/* posizione è un puntatore a cella */
typedef Cella<T>* posizione;
typedef T tipoelem;
/* Prototipi degli operatori */
void crealista();
bool listaVuota() const;
tipoelem leggiLista(posizione) const;
void scriviLista(tipoelem, posizione);
posizione primoLista() const;
bool fineLista(posizione) const;
posizione succLista(posizione) const;
posizione precLista(posizione) const;
void insLista(tipoelem,posizione);
void cancLista(posizione);
// funzioni di servizio
private:
posizione lista; //la lista è un puntatore ad oggetto Cella
};
/* Liste: Rappresentazione collegata circolare (con sentinella)
* realizzata mediante doppi puntatori (o simmetrica)
*/
template <class T> circLista<T>::circLista()
{crealista();}
template <class T> circLista<T>::~circLista()
{
while (lista->getSucc() != lista->getPrec())
{
Cella<T>* posizione = lista->getSucc();
cancLista(posizione);
}
delete lista;
}
template <class T> void circLista<T>::crealista()
{
T ElementoNullo;
lista = new Cella<T>;
lista->setElemento(ElementoNullo);
lista->setSucc(lista);
lista->setPrec(lista);
//la sentinella punta a se stessa
}
template <class T> bool circLista<T>::listaVuota() const
{
return ((lista->getSucc() == lista) && (lista->getPrec()==lista));
}
template <class T> Cella<T>* circLista<T>::primoLista() const
{
return lista->getSucc();
}
template <class T> Cella<T>* circLista<T>::succLista(posizione p) const
{
return p->getSucc();
}
template <class T> Cella<T>* circLista<T>::precLista(posizione p) const
{
return p->getPrec();
}
template <class T> bool circLista<T>::fineLista(posizione p) const
{
return (p==lista);
}
template <class T> T circLista<T>::leggiLista(posizione p) const
{
return p->getElemento();
}
template <class T> void circLista<T>::scriviLista(tipoelem a, posizione p)
{
p->setElemento(a);
}
template <class T> void circLista<T>::insLista(tipoelem a, posizione p)
{
Cella<T>* temp = new Cella<T>;
temp->setElemento(a);
temp->setPrec(p->getPrec());
temp->setSucc(p);
(p->getPrec())->setSucc(temp);
p->setPrec(temp);
p=temp; // se p era la posizione dell'elemento n-mo, adesso lo è temp
}
template <class T> void circLista<T>::cancLista(posizione p)
{
Cella<T>* temp = new Cella<T>;
temp=p;
(p->getSucc())->setPrec(p->getPrec());
(p->getPrec())->setSucc(p->getSucc());
p=p->getSucc();
delete(temp);
}
Sorry for the language used in the code comments (it's italian).
Could someone explain me how to deal with these errors?