PDA

View Full Version : [c++] Errore costruttore di copia per alberi binari


mirkus90
10-12-2014, 20:40
Allora vi spiego subito il mio problema. Sto realizzando la struttura degli Alberi Binari mediante l'uso dei puntatori in c++. Unico problema che ora quando scrivo il costruttore di copia della mia classe mi da errore
.../bin_albero/BinAlbero_punt.h|66|error: passing ‘const BinAlbero_punt<int>’ as ‘this’ argument of ‘Nodo<tipoelem>* BinAlbero_punt<tipoelem>::binRadice() [with tipoelem = int; BinAlbero_punt<tipoelem>::posizione = Nodo<int>*]’ discards qualifiers [-fpermissive]|

Questa è la mia classe BinAlbero_punt


template <typename tipoelem>
class BinAlbero_punt: public BinAlbero<tipoelem>
{

public:

typedef Nodo<tipoelem>* posizione;


BinAlbero_punt(); //OK
BinAlbero_punt(const BinAlbero_punt<tipoelem>&);
~BinAlbero_punt(); //OK

void creaBinAlbero(); //OK
bool binAlberoVuoto(); //OK
posizione binRadice(); //OK
posizione binPadre( posizione); //OK
posizione figlioSinistro( posizione); //OK
posizione figlioDestro( posizione); //OK
bool sinistroVuoto( posizione); //OK
bool destroVuoto( posizione); //OK
void costrBinAlbero(BinAlbero_punt<tipoelem>,BinAlbero_punt<tipoelem>);
void cancSottoBinAlbero(posizione); //OK
void insRadice(posizione); //OK
void insFiglioSinistro(tipoelem,posizione); //OK
void insFiglioDestro(tipoelem,posizione); //OK
tipoelem leggiNodo(posizione);
void scriviNodo(tipoelem, posizione);

void vistaPostOrdine( posizione); //OK
void vistaPreOrdine( posizione); //OK
void vistaSimmetrica( posizione); //OK
posizione copiaSottoAlbero(posizione);


private:
posizione albero;
};

template <typename tipoelem>
BinAlbero_punt<tipoelem>::BinAlbero_punt()
{
this->creaBinAlbero();
return;
}

template <typename tipoelem>
BinAlbero_punt<tipoelem>::~BinAlbero_punt()
{
this->cancSottoBinAlbero(albero);

return;
}

template <typename tipoelem>
BinAlbero_punt<tipoelem>::BinAlbero_punt(const BinAlbero_punt<tipoelem>& albero_ext)
{
this->creaBinAlbero();
albero=copiaSottoAlbero(albero_ext.binRadice());
return;
}

template <typename tipoelem>
void BinAlbero_punt<tipoelem>::creaBinAlbero()
{
albero=new Nodo<tipoelem>;
return;
}


template <typename tipoelem>
bool BinAlbero_punt<tipoelem>::binAlberoVuoto()
{
return(albero->getFiglioDx()==nullptr && albero->getFiglioSx()==nullptr && albero->getPadre()==nullptr&& albero->getElemento()==NULL);
}


template <typename tipoelem>
typename BinAlbero_punt<tipoelem>::posizione BinAlbero_punt<tipoelem>::binRadice()
{
if(!this->binAlberoVuoto())
return albero;
else
cout<<"ATTENZIONE ALBERO VUOTO, NON SI PUO' RESTITTUIRE LA RADICE!!!"<<endl;
}

template <typename tipoelem>
typename BinAlbero_punt<tipoelem>::posizione BinAlbero_punt<tipoelem>::binPadre( posizione nodo_ext)
{
if(nodo_ext->getPadre()!=nullptr)
return nodo_ext->getPadre();
else
cout<<"ATTENZIONE IL NODO NON HA UN PADRE!!!"<<endl;
}

template <typename tipoelem>
typename BinAlbero_punt<tipoelem>::posizione BinAlbero_punt<tipoelem>::figlioDestro( posizione nodo_ext)
{
if(!this->destroVuoto(nodo_ext))
return nodo_ext->getFiglioDx();
//else
// cout<<"ATTENZIONE IL NODO NON HA FIGLIO DESTRO!!!"<<endl;

return nullptr;
}

template <typename tipoelem>
typename BinAlbero_punt<tipoelem>::posizione BinAlbero_punt<tipoelem>::figlioSinistro( posizione nodo_ext)
{
if(!this->sinistroVuoto(nodo_ext))
return nodo_ext->getFiglioSx();
//else
// cout<<"ATTENZIONE IL NODO NON HA FIGLIO SINISTRO!!!"<<endl;

return nullptr;
}


template <typename tipoelem>
bool BinAlbero_punt<tipoelem>::destroVuoto( posizione nodo_ext)
{
return(nodo_ext->getFiglioDx()==nullptr);
}

template <typename tipoelem>
bool BinAlbero_punt<tipoelem>::sinistroVuoto( posizione nodo_ext)
{
return(nodo_ext->getFiglioSx()==nullptr);
}

template <typename tipoelem>
void BinAlbero_punt<tipoelem>::insFiglioDestro(tipoelem elem, posizione nodo_albero)
{
Nodo<tipoelem>* temp=new Nodo<tipoelem>;
temp->setElemento(elem);
temp->setPadre(nodo_albero);
nodo_albero->setFiglioDx(temp);

// cout<<"IMPOSSIBILE AGGIUNGERE FIGLIO DESTRO"<<endl;
}

template <typename tipoelem>
void BinAlbero_punt<tipoelem>::insFiglioSinistro(tipoelem elem, posizione nodo_albero)
{
Nodo<tipoelem>* temp=new Nodo<tipoelem>;
temp->setElemento(elem);
temp->setPadre(nodo_albero);
nodo_albero->setFiglioSx(temp);

// cout<<"IMPOSSIBILE AGGIUNGERE FIGLIO DESTRO"<<endl;
}

template <typename tipoelem>
void BinAlbero_punt<tipoelem>::insRadice(posizione elem)
{
albero=elem;
return;
}

template <typename tipoelem>
void BinAlbero_punt<tipoelem>::cancSottoBinAlbero(posizione nodo_ext)
{

Nodo<tipoelem>* nodo_temp=nodo_ext->getFiglioSx();
if(nodo_temp!=nullptr)
this->cancSottoBinAlbero(nodo_temp);

nodo_temp=nodo_ext->getFiglioDx();
if(nodo_temp!=nullptr)
this->cancSottoBinAlbero(nodo_temp);

nodo_ext->~Nodo();
return;
}

template <typename tipoelem>
void BinAlbero_punt<tipoelem>::vistaPostOrdine( posizione nodo_ext)
{

if(this->sinistroVuoto(nodo_ext) && this->destroVuoto(nodo_ext))
{
tipoelem elem=nodo_ext->getElemento();
cout<<elem<<" - ";
}
else
{
Nodo<tipoelem>* nodo_temp=nodo_ext->getFiglioSx();
if(nodo_temp!=nullptr)
this->vistaPostOrdine(nodo_temp);

nodo_temp=nodo_ext->getFiglioDx();
if(nodo_temp!=nullptr)
this->vistaPostOrdine(nodo_temp);

tipoelem elem=nodo_ext->getElemento();
cout<<elem<<" - ";

}
}



template <typename tipoelem>
void BinAlbero_punt<tipoelem>::vistaPreOrdine( posizione nodo_ext)
{

tipoelem elem=nodo_ext->getElemento();
cout<<elem<<" - ";

Nodo<tipoelem>* nodo_temp=nodo_ext->getFiglioSx();
if(nodo_temp!=nullptr)
this->vistaPreOrdine(nodo_temp);

nodo_temp=nodo_ext->getFiglioDx();
if(nodo_temp!=nullptr)
this->vistaPreOrdine(nodo_temp);

}

template <typename tipoelem>
void BinAlbero_punt<tipoelem>::vistaSimmetrica( posizione nodo_ext)
{

Nodo<tipoelem>* nodo_temp=nodo_ext->getFiglioSx();
if(nodo_temp!=nullptr)
this->vistaSimmetrica(nodo_temp);

tipoelem elem=nodo_ext->getElemento();
cout<<elem<<" - ";

nodo_temp=nodo_ext->getFiglioDx();
if(nodo_temp!=nullptr)
this->vistaSimmetrica(nodo_temp);

}

template <typename tipoelem>
tipoelem BinAlbero_punt<tipoelem>::leggiNodo(posizione nodo_ext)
{
return nodo_ext->getElemento();
}

template <typename tipoelem>
void BinAlbero_punt<tipoelem>::scriviNodo(tipoelem elem, posizione nodo_ext)
{
nodo_ext->setElemento(elem);
return;
}

template <typename tipoelem>
typename BinAlbero_punt<tipoelem>::posizione BinAlbero_punt<tipoelem>::copiaSottoAlbero(posizione nodo_ext)
{
posizione nodo_temp;

if (nodo_ext==nullptr)
return nullptr;
else
{
nodo_temp = new Nodo<tipoelem>;
scriviNodo(leggiNodo(nodo_ext), nodo_temp);

if (this->figlioSinistro(nodo_ext))
nodo_temp->setFiglioSx(copiaSottoAlbero(figlioSinistro(nodo_ext)));

if (this->figlioDestro(nodo_ext))
nodo_temp->setFiglioDx(copiaSottoAlbero(figlioDestro(nodo_ext)));
}
return nodo_temp;
}


template <typename tipoelem>
void BinAlbero_punt<tipoelem>::costrBinAlbero(BinAlbero_punt<tipoelem>alberoSx,BinAlbero_punt<tipoelem> alberoDx)
{

if(!this->binAlberoVuoto())
this->cancSottoBinAlbero(this->binRadice());
//this->creaBinAlbero();
albero->setElemento(NULL);
this->vistaPostOrdine(albero);

albero->setFiglioDx(copiaSottoAlbero(alberoDx.binRadice()));
albero->setFiglioSx(copiaSottoAlbero(alberoSx.binRadice()));

}


E questa la classe Nodo

Nel caso vi servisse la classe Nodo ve la posto.
vi ringrazio in anticipo

vendettaaaaa
10-12-2014, 21:15
Molto semplice: invochi su un oggetto const (l'argomento del copy constructor) una funzione membro di quella classe che NON è dichiarata const.
Vedo che non hai dichiarato const alcuna delle tue funzioni, quindi probabilmente non sai come usare questa caratteristica: quando definisci una classe, puoi aggiungere la keyword const dopo l'elenco dei parametri, per indicare che quella funzione non modifica lo stato degli oggetti di quella classe (cioè non va a scrivere le variabili membro).
Tu stai invocando binRadice() sull'oggetto const che passi al copy constructor, ma dato che binRadice non è const, il compilatore non sa che essa non modifica lo stato dell'oggetto e da errore perchè se l'oggetto è const, immutabile, non puoi invocare funzioni non-const.

Nella dichiarazione della classe, devi scrivere quindi:
posizione binRadice() const; //OK

Tieni conto che const va riportata anche nella definizione della funzione:
template <typename tipoelem>
typename BinAlbero_punt<tipoelem>::posizione BinAlbero_punt<tipoelem>::binRadice() const
{
if(!this->binAlberoVuoto())
return albero;
else
cout<<"ATTENZIONE ALBERO VUOTO, NON SI PUO' RESTITTUIRE LA RADICE!!!"<<endl;
}

poichè const modifica la firma della funzione (cioè, due funzioni con ugual nome e uguale lista di parametri sono diverse se una è const e l'altra no).

Tieni conto che, logicamente, nelle funzioni const non puoi scrivere variabili membro nè invocare funzioni membro non const.

mirkus90
10-12-2014, 21:58
A dirla tutta questa caratteristica dell'aggiunta del const la conosoco, ma forse ho il brutto vizio di aggiungere alla fine della mia scrittura questi "particolari"...poi avendo avuto l'errore mi sono infognato li e non ci ho più pensato...ora funziona tutto...vedrò ti togliermi questo viziaccio...grazieeeeeee