C ++覆盖了抽象模板化类的错误

时间:2014-11-08 14:38:39

标签: c++ compiler-errors

我正在尝试构建自定义列表实现(用于练习)。目前我已经创建了一个带游标的实现,通常用指针创建一个实现(循环双向链表)。我不想创建不同的通用函数(打印列表,自然合并排序等),其中唯一的区别是所使用的类)。所以我用纯虚方法创建了一个抽象类,将它用作接口。我使用条件编译来处理位置类型的不同实现(游标实现中的int和指向循环双向链表中的节点的指针)。但是我有这些错误:

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);
}

很抱歉代码评论中使用的语言(意大利语)。

有人可以解释我如何处理这些错误吗?

1 个答案:

答案 0 :(得分:0)

我&#34;解决了#34;问题是在eclipse-cdt中创建另一个项目文件夹并仅导入我在特定测试中需要的文件(TestMain.cpp使用抽象类Lista,派生类circLista而不是{{1所以我不需要在同一个项目中同时使用它们。我多次查看旧项目文件夹中的ListaCursori语句,似乎没有任何错误。我认为Eclipse可能会在编译或链接过程中混乱(使用#includeListaCursori,因此会出现覆盖错误)。由于我在工具链编辑器设置中使用circLista(来源:Where is the Makefile generated by the Eclipse CDT?),因此没有makefile可供分析以便在Debug或Release文件夹中进一步调查。 无论如何,我不会感到惊讶,因为这不是Eclipse第一次做到这一点,并且#34;奇怪&#34; Windows上的东西(如果真的是它的错)。 感谢大家试图帮助我,即使主要问题不是那么清楚。

相关问题