| 
 | |||||||
| 
 | 
|  | 
|  | 
|  | Strumenti | 
|  09-12-2008, 15:40 | #1 | 
| Junior Member Iscritto dal: Aug 2007 
					Messaggi: 22
				 | 
				
				[C++] Alberi binari di ricerca
			 
		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: 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;
}Codice: 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 prego aiutatemi...sono nel panico più totale...   | 
|   |   | 
|  09-12-2008, 16:27 | #2 | 
| Senior Member Iscritto dal: Nov 2005 Città: Texas 
					Messaggi: 1722
				 | 
		Il compilatore si arrabbia per qualcosa che e' nel main()... potresti postarlo?
		 
				__________________ In God we trust; all others bring data | 
|   |   | 
|  09-12-2008, 16:59 | #3 | |
| Junior Member Iscritto dal: Aug 2007 
					Messaggi: 22
				 | Quote: 
 Codice: #include "Element.h"
int main(){
char a('q');
Element<char> e(); 
e= a;
std::cout<<e;
return 0;
} | |
|   |   | 
|  09-12-2008, 17:25 | #4 | |
| Senior Member Iscritto dal: Nov 2005 Città: Texas 
					Messaggi: 1722
				 | Quote: 
 Prova a togliere le parentesi ()... 
				__________________ In God we trust; all others bring data | |
|   |   | 
|  09-12-2008, 18:13 | #5 | 
| Senior Member Iscritto dal: Nov 2008 
					Messaggi: 530
				 | 
		per quanto riguarda questo  Codice: static unsigned int next_id=0; e potresti fare in queto modo. fuori dalla classe Element Codice: template<class T> unsigned int Element<T>::next_id = 0; fammi sapere | 
|   |   | 
|  09-12-2008, 18:23 | #6 | |
| Senior Member Iscritto dal: Nov 2008 
					Messaggi: 530
				 | Quote: 
  ") di una funzione (cosa che non è). | |
|   |   | 
|  09-12-2008, 21:15 | #7 | |
| Junior Member Iscritto dal: Aug 2007 
					Messaggi: 22
				 | Quote: 
   | |
|   |   | 
|  09-12-2008, 21:33 | #8 | 
| Junior Member Iscritto dal: Aug 2007 
					Messaggi: 22
				 | 
		Niente...   il problema della variabile static c'è ancora (mi da proprio lo stesso errore di prima) E l'altro problema permane si è trasformato in un: Codice: 35 missing template arguments before ';' token   | 
|   |   | 
|  09-12-2008, 21:40 | #9 | 
| Junior Member Iscritto dal: Aug 2007 
					Messaggi: 22
				 | 
		Avevo pensato che forse i "missing bla bla..." erano dovuti a next_id++; senza Element<T>::next_id++; Ma se ce lo metto l'errore resta!
		 | 
|   |   | 
|  09-12-2008, 22:55 | #10 | 
| Senior Member Iscritto dal: Nov 2005 Città: Texas 
					Messaggi: 1722
				 | 
		Puoi postare la nuova lista di errori/warning?
		 
				__________________ In God we trust; all others bring data | 
|   |   | 
|  09-12-2008, 23:03 | #11 | 
| Senior Member Iscritto dal: Nov 2008 
					Messaggi: 530
				 | 
		A me così parte e non da nessun errore provalo Codice: #include <iostream>
#include <math.h>
template <class T>
class Element{
  private:
	unsigned int id;
	static unsigned int next_id;
	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;
}
template <class T>
unsigned int Element<T>::next_id = 0;
int main()
{
	char a('q');
	Element<char> e; 
	e= a;
	std::cout<<e;
	return 0;
} | 
|   |   | 
|  10-12-2008, 14:10 | #12 | |
| Junior Member Iscritto dal: Aug 2007 
					Messaggi: 22
				 | Quote: 
    Grazie mille. Cmq si, l'avevo messo su element.h all'interno...chissà perchè in quel modo non gli piace al compilatore... Grazie ancora, non sai quanto mi hai aiutato così!! (Se avessi altri problemi li posto)   | |
|   |   | 
|  10-12-2008, 18:18 | #13 | |
| Senior Member Iscritto dal: Nov 2008 
					Messaggi: 530
				 | Quote: 
   | |
|   |   | 
|   | 
| Strumenti | |
| 
 | 
 | 
Tutti gli orari sono GMT +1. Ora sono le: 04:31.









 
		 
		 
		 
		







 
  
 



 
                        
                        










