Baronerosso9
09-12-2008, 15:40
Ciao a tutti.
Spero che qualcuno mi possa dare una mano...ho un esame tra una settimana e devo consegnare un progettino che il compilatore non ne vuole sapere di compilare. Vi spiego...
Devo fare un dizionario implementato con un albero binario di ricerca. Per adesso sto costruendo la classe "Element" che va a rappresentare un singolo nodo.(Lo implemento coi templates...)
Ora...voglio dare un ID univoco ad ogni nodo...ho pensato di farlo usando una variabile statica inizializzata a zero. Xò il compilatore mi da errore, dice ke è proibito inizializzare una variabile statica...(cosa ke in Java facevo tranquillamente).
Un altro errore me lo da quando provo a definirgli un operatore di copia che mi renda valido fare una cosa del tipo: oggetto_elemento<rational>= numero_razionle.
Ma mi da errore...ho provato in diversi modi, ma continua a darmi errori...
Vi scrivo il codice:
#include <iostream>
#include <math.h>
template <class T>
class Element{
private:
unsigned int id;
static unsigned int next_id=0;
T* value;
Element* Dad;
Element* Right;
Element* Left;
public:
Element(){id=next_id; next_id++; Dad=NULL; Left=NULL; Right=NULL; value = NULL;}
Element& operator=(T& n){ //mi permette di fare elemento<rational>= razionale;
if (&n==value)
return *this;
if (value!=NULL)
delete(value);
value = new T(n);
return *this;
}
Element(T& n){id=next_id; next_id++; Dad=NULL; Left=NULL; Right=NULL; &this=n; }
/* operator= da definire per T */
//?Element(T* ptr){id=next_id; next_id++; Dad=NULL; Left=NULL; Right=NULL; value=*ptr;}
Element(const Element& n){
id=next_id; next_id++; Dad=NULL; Left=NULL; Right=NULL; &this=n.value; }
unsigned int Key()const{return id;}
T data()const{ return *value;}
Element* dx() const{return Right;}
Element* sx() const{return Left;}
Element* px() const{return Dad;}
bool operator==(const Element& p) const{return id==p.id;}
bool operator>(const Element& el){
return id>el.id;
}
bool operator>=(const Element& el){
return id>=el.id;
}
bool operator<=(const Element& el){
return id<=el.id;
}
bool operator<(const Element& el){
return id<el.id;
}
bool operator!=(const Element& el){
return id!=el.id;
}
};
template<class T>
std::ostream& operator<<(std::ostream& os, const Element<T>& p){
os << p.data(); //Da definire l'operator<< per T
return os;
}
Gli errori che mi da sono questi:
Element.h:13: error: ISO C++ forbids in-class initialization of non-const static member ‘next_id’
main.cpp: In function ‘int main()’:
main.cpp:6: error: assignment of function ‘Element<char> e()’
main.cpp:6: error: cannot convert ‘char’ to ‘Element<char> ()()’ in assignment
main.cpp:7: warning: the address of ‘Element<char> e()’, will always evaluate as ‘true’
make: *** [main] Error 1
Vi ringrazio infinitamente!!
Vi prego aiutatemi...sono nel panico più totale...:help:
Spero che qualcuno mi possa dare una mano...ho un esame tra una settimana e devo consegnare un progettino che il compilatore non ne vuole sapere di compilare. Vi spiego...
Devo fare un dizionario implementato con un albero binario di ricerca. Per adesso sto costruendo la classe "Element" che va a rappresentare un singolo nodo.(Lo implemento coi templates...)
Ora...voglio dare un ID univoco ad ogni nodo...ho pensato di farlo usando una variabile statica inizializzata a zero. Xò il compilatore mi da errore, dice ke è proibito inizializzare una variabile statica...(cosa ke in Java facevo tranquillamente).
Un altro errore me lo da quando provo a definirgli un operatore di copia che mi renda valido fare una cosa del tipo: oggetto_elemento<rational>= numero_razionle.
Ma mi da errore...ho provato in diversi modi, ma continua a darmi errori...
Vi scrivo il codice:
#include <iostream>
#include <math.h>
template <class T>
class Element{
private:
unsigned int id;
static unsigned int next_id=0;
T* value;
Element* Dad;
Element* Right;
Element* Left;
public:
Element(){id=next_id; next_id++; Dad=NULL; Left=NULL; Right=NULL; value = NULL;}
Element& operator=(T& n){ //mi permette di fare elemento<rational>= razionale;
if (&n==value)
return *this;
if (value!=NULL)
delete(value);
value = new T(n);
return *this;
}
Element(T& n){id=next_id; next_id++; Dad=NULL; Left=NULL; Right=NULL; &this=n; }
/* operator= da definire per T */
//?Element(T* ptr){id=next_id; next_id++; Dad=NULL; Left=NULL; Right=NULL; value=*ptr;}
Element(const Element& n){
id=next_id; next_id++; Dad=NULL; Left=NULL; Right=NULL; &this=n.value; }
unsigned int Key()const{return id;}
T data()const{ return *value;}
Element* dx() const{return Right;}
Element* sx() const{return Left;}
Element* px() const{return Dad;}
bool operator==(const Element& p) const{return id==p.id;}
bool operator>(const Element& el){
return id>el.id;
}
bool operator>=(const Element& el){
return id>=el.id;
}
bool operator<=(const Element& el){
return id<=el.id;
}
bool operator<(const Element& el){
return id<el.id;
}
bool operator!=(const Element& el){
return id!=el.id;
}
};
template<class T>
std::ostream& operator<<(std::ostream& os, const Element<T>& p){
os << p.data(); //Da definire l'operator<< per T
return os;
}
Gli errori che mi da sono questi:
Element.h:13: error: ISO C++ forbids in-class initialization of non-const static member ‘next_id’
main.cpp: In function ‘int main()’:
main.cpp:6: error: assignment of function ‘Element<char> e()’
main.cpp:6: error: cannot convert ‘char’ to ‘Element<char> ()()’ in assignment
main.cpp:7: warning: the address of ‘Element<char> e()’, will always evaluate as ‘true’
make: *** [main] Error 1
Vi ringrazio infinitamente!!
Vi prego aiutatemi...sono nel panico più totale...:help: