연습용으로 사용자 지정 목록 구현을 작성하려고합니다. 현재 나는 커서와 포인터 (원형 이중 연결리스트)를 가진 일반적인 구현을 만들었습니다. 유일한 차이점은 사용되는 클래스가 다른 일반 함수 (인쇄 목록, 자연 병합 정렬 등)를 만들고 싶지 않습니다. 그래서 순수 가상 메서드를 사용하여 추상 클래스를 만들어 인터페이스로 사용했습니다. 조건부 컴파일을 사용하여 위치 유형 (커서 구현의 int 및 순환 이중 연결 목록의 노드에 대한 포인터)의 다른 구현을 처리했습니다. 그러나 나는이 오류가있다 :추상 템플릿 기반 클래스에서 C++ 재정의 오류
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>*]'
나는 주 파일을 쓸 것이다.
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);
}
코드 주석 (는 이탈리아어입니다)에서 사용되는 언어에 대한 죄송합니다.
누군가 이러한 오류를 처리하는 방법을 설명해 주시겠습니까?
나는 의견을 이해하지만 다른 사람들은 그렇지 않을 수도 있습니다. 어쨌든 너무 많은 코드를 게시 한 경우 문제를 줄이고 여기에 관련 부분을 게시해야합니다. –
오류의 일부만 붙여 넣어 중요한 부분을 빠져 나갔습니다. – jrok
컴파일러에 따르면이 오류는 특히 Lista.h의 30 행에 있습니다. "virtual posizione primoLista() const = 0;" TestMain.cpp를 게시하면 도움이 될지 모르겠다. 이미 너무 많은 코드를 게시 했으므로 지적했다. – DarthVi