Pagina 1 di 3 1 2 3 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 23
  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2002
    Messaggi
    1,202

    [C++] Alberi e overloading

    Ciao, ho compilato questo programma sugli alberi binari, e mi escono alcuni errori che non capisco: in particolare mi dice "ambiguous call to overloaded function" riferendosi alla ricorsività nella funzione InsAlbero e nella funzione DistruggiAlbero; a cosa può essere dovuto e come posso risolvere?

    Grazie

    codice:
    #include <iostream.h>
    #include <stdlib.h>
    
    struct treenode{
    	int info;
    	treenode* sx;
    	treenode* dx;
    	};
    
    int ContaNodi(treenode*);
    treenode* BilanciaAlbero(treenode*);
    void CreaArray(treenode*,int,int[]);
    treenode* RicavaAlbero(int,int,int[]);
    void InsAlbero(treenode*,int);
    void DistruggiAlbero(treenode*);
    void StampaAlbero(treenode*);
    
    void main()
    {
    	treenode* albero=NULL;
    	int scelta,n;
    	char end;
    	while(scelta!=0)
    	{
    		system("cls");
    		cout<<"\n\t\t\tMENU"
    			<<"\n\t|1|  Crea albero"
    			<<"\n\t|2|  Visualizza albero"
    			<<"\n\t|0|  Esci"
    			<<"\n\n\t\tOpzione: ";
    		cin>>scelta;
    
    		switch(scelta){
    		case 1:
    			while(n!=0)
    			{
    				cout<<"\n\tElemento: "; 
    				cin>>n;
    				InsAlbero(albero,n);
    			}
    			BilanciaAlbero(albero);
    			break;
    		case 2:
    			cout<<"\n\n";
    			StampaAlbero(albero);
    			cout<<"\n\tPremi un tasto per tornare al menu ";
    			cin>>end;
    			break;
    		}
    	}
    }
    
    void InsAlbero(treenode* &radice,int n)
    {
    	if(radice==NULL)
    	{
    		radice=new treenode;
    		radice->info=n;
    		radice->sx=NULL;
    		radice->dx=NULL;
    	}
    
    	else
    	{
    		if(n>radice->info)
    		{
    			if(radice->dx==NULL)
    			{
    				radice->dx=new treenode;
    				radice->dx->info=n;
    				radice->dx->dx=NULL;
    				radice->dx->sx=NULL;
    			}
    			else
    				InsAlbero(radice->dx,n);
    		}
    
    		else if(n<radice->info)
    		{
    			if(radice->sx==NULL)
    			{
    				radice->sx=new treenode;
    				radice->sx->info=n;
    				radice->sx->dx=NULL;
    				radice->sx->sx=NULL;
    			}
    			else
    				InsAlbero(radice->sx,n);
    		}
    	}
    }
    
    void BilanciaAlbero(treenode* &radice)
    {
    	int DIM;
    	DIM = ContaNodi(radice);
    	int array = new int[DIM];
    	int i=0;
    	CreaArray(radice,array,i);
    	DistruggiAlbero(radice);
    	int p=1;
    	int u=DIM;
    	radice=RicavaAlbero(p,u,array);
    }
    
    int ContaNodi(treenode* radice)
    {
    	int cont=0;
    
    	if(radice->dx!=NULL)
    		cont=ContaNodi(radice->dx);
    
    	if(radice->sx!=NULL)
    		cont=ContaNodi(radice->sx);
    	
    	cont++;
    
    	return cont;
    }
    
    void CreaArray(treenode* radice,int array[],int &i)
    {
    	if(radice->sx!=NULL)
    		CreaArray(radice->sx,array,i);
    	
    	array[i]=radice->info;
    	i++;
    
    	if(radice->dx!=NULL)
    		CreaArray(radice->dx,array,i);
    }
    
    void DistruggiAlbero(treenode* &radice)
    {
    	if(radice!=NULL)
    	{
    		if(radice->sx!=NULL)
    			DistruggiAlbero(radice->sx);
    		if(radice->dx!=NULL)
    			DistruggiAlbero(radice->dx);
    
    		delete radice;
    		radice=NULL;
    	}	
    }
    
    treenode* RicavaAlbero(int primo, int ultimo, int array[])
    {
    	int mediana;
    	if(ultimo>=primo)
    	{
    		mediana=(primo+ultimo)/2;
    		treenode* tmp=new treenode;
    		tmp->info=array[mediana];
    		tmp->sx=RicavaAlbero(primo,mediana-1,array);
    		tmp->dx=RicavaAlbero(mediana+1,ultimo,array);
    		return tmp;
    	}
    	return NULL;
    }
    
    void StampaAlbero(treenode* radice)
    {
    	if(radice!=NULL)
    	{
    		cout<<"\t"<<radice->info;
    		StampaAlbero(radice->sx);
    		StampaAlbero(radice->dx);
    	}
    }
    Debian GNU/Linux sid
    Publishing a theory should not be the end of one's conversation with the universe, but the beginning. (Eric S. Raymond)
    Kernel 2.6.14-ck1

  2. #2
    fai queste sostituzioni:

    void InsAlbero(treenode* &radice,int n) ---> void InsAlbero(treenode *radice, int n)


    void DistruggiAlbero(treenode* &radice) ---> void DistruggiAlbero(treenode *radice)


    e la vita ti sorridera' (o almeno spero)

    bye
    There are 10 kinds of people in the world: who knows the binary numeration and who not

  3. #3
    Utente di HTML.it
    Registrato dal
    Feb 2002
    Messaggi
    1,202
    Avevo sbagliato a scrivere i prototipi, che beota :adhone:

    Grazie, ora c'è solo un errore
    (Linea 97) : error C2440: 'initializing' : cannot convert from 'int *' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    La riga in questione è

    int array = new int[DIM];

    All'interno della funzione BilanciaAlbero...
    Non capisco
    Debian GNU/Linux sid
    Publishing a theory should not be the end of one's conversation with the universe, but the beginning. (Eric S. Raymond)
    Kernel 2.6.14-ck1

  4. #4
    Se vuoi un int* o usi un casting o li dichiari come int e basta.

    .:: Zetra.it - Web. ads . multimedia . graphix ::.
    Realizzazione siti web - Carte Magic ai prezzi più bassi d'italia
    - Comuni e Città

  5. #5
    Utente di HTML.it
    Registrato dal
    Feb 2002
    Messaggi
    1,202
    ma io non capisco cosa deve convertire

    voglio solo creare un nuovo array di interi di DIM elementi

    il fatto è che è copiato pari-pari dall'esercizio di scuola

    int Vett = new int [Elementi];

    Debian GNU/Linux sid
    Publishing a theory should not be the end of one's conversation with the universe, but the beginning. (Eric S. Raymond)
    Kernel 2.6.14-ck1

  6. #6
    Eccolo l'errore:

    tmp->info=array[mediana];

    Non puoi mettere un array in un int!

    .:: Zetra.it - Web. ads . multimedia . graphix ::.
    Realizzazione siti web - Carte Magic ai prezzi più bassi d'italia
    - Comuni e Città

  7. #7
    errore molto grave...

    in pratica l'operazione che fai assegna ad una variabile un indirizzo di memoria, ecco perche' ti dice di castare. Per ovviare al problema (e spero sia quello che vuoi):

    int *array = new int[DIM];

    e la vita ti sorridera' di nuovo

    bye
    There are 10 kinds of people in the world: who knows the binary numeration and who not

  8. #8
    punkivi, per piacere non dire cazzate. Non per un qualche motivo particolare, ma perche' potresti confondere la gente. Forse prima di parlare si farebbe bene a leggere un libro sul C++, che ne pensi?

    bye
    There are 10 kinds of people in the world: who knows the binary numeration and who not

  9. #9
    Utente di HTML.it
    Registrato dal
    Feb 2002
    Messaggi
    1,202
    Non ci sto capendo più niente
    facendo quella correzione, ho dovuto farne altre, a questo punto il codice intero è così:

    codice:
    #include <iostream.h>
    #include <stdlib.h>
    
    struct treenode{
    	int info;
    	treenode* sx;
    	treenode* dx;
    	};
    
    int ContaNodi(treenode*);
    treenode* BilanciaAlbero(treenode* &);
    void CreaArray(treenode*,int*,int&);
    treenode* RicavaAlbero(int,int,int*);
    void InsAlbero(treenode* &,int);
    void DistruggiAlbero(treenode* &);
    void StampaAlbero(treenode*);
    
    void main()
    {
    	treenode* albero=NULL;
    	int scelta,n;
    	char end;
    	while(scelta!=0)
    	{
    		system("cls");
    		cout<<"\n\t\t\tMENU"
    			<<"\n\t|1|  Crea albero"
    			<<"\n\t|2|  Visualizza albero"
    			<<"\n\t|0|  Esci"
    			<<"\n\n\t\tOpzione: ";
    		cin>>scelta;
    
    		switch(scelta){
    		case 1:
    			while(n!=0)
    			{
    				cout<<"\n\tElemento: "; 
    				cin>>n;
    				InsAlbero(albero,n);
    			}
    			BilanciaAlbero(albero);
    			break;
    		case 2:
    			cout<<"\n\n";
    			StampaAlbero(albero);
    			cout<<"\n\tPremi un tasto per tornare al menu ";
    			cin>>end;
    			break;
    		}
    	}
    }
    
    void InsAlbero(treenode* &radice,int n)
    {
    	if(radice==NULL)
    	{
    		radice=new treenode;
    		radice->info=n;
    		radice->sx=NULL;
    		radice->dx=NULL;
    	}
    
    	else
    	{
    		if(n>radice->info)
    		{
    			if(radice->dx==NULL)
    			{
    				radice->dx=new treenode;
    				radice->dx->info=n;
    				radice->dx->dx=NULL;
    				radice->dx->sx=NULL;
    			}
    			else
    				InsAlbero(radice->dx,n);
    		}
    
    		else if(n<radice->info)
    		{
    			if(radice->sx==NULL)
    			{
    				radice->sx=new treenode;
    				radice->sx->info=n;
    				radice->sx->dx=NULL;
    				radice->sx->sx=NULL;
    			}
    			else
    				InsAlbero(radice->sx,n);
    		}
    	}
    }
    
    void BilanciaAlbero(treenode* &radice)
    {
    	int DIM;
    	DIM = ContaNodi(radice);
    	int* array = new int[DIM];
    	int i=0;
    	CreaArray(radice,array,i);
    	DistruggiAlbero(radice);
    	int p=1;
    	int u=DIM;
    	radice=RicavaAlbero(p,u,array);
    }
    
    int ContaNodi(treenode* radice)
    {
    	int cont=0;
    
    	if(radice->dx!=NULL)
    		cont=ContaNodi(radice->dx);
    
    	if(radice->sx!=NULL)
    		cont=ContaNodi(radice->sx);
    	
    	cont++;
    
    	return cont;
    }
    
    void CreaArray(treenode* radice,int* array,int &i)
    {
    	if(radice->sx!=NULL)
    		CreaArray(radice->sx,array,i);
    	
    	array[i]=radice->info;
    	i++;
    
    	if(radice->dx!=NULL)
    		CreaArray(radice->dx,array,i);
    }
    
    void DistruggiAlbero(treenode* &radice)
    {
    	if(radice!=NULL)
    	{
    		if(radice->sx!=NULL)
    			DistruggiAlbero(radice->sx);
    		if(radice->dx!=NULL)
    			DistruggiAlbero(radice->dx);
    
    		delete radice;
    		radice=NULL;
    	}	
    }
    
    treenode* RicavaAlbero(int primo, int ultimo, int* array)
    {
    	int mediana;
    	if(ultimo>=primo)
    	{
    		mediana=(primo+ultimo)/2;
    		treenode* tmp=new treenode;
    		tmp->info=array[mediana];
    		tmp->sx=RicavaAlbero(primo,mediana-1,array);
    		tmp-> dx=RicavaAlbero(mediana+1,ultimo,array);
    
    		return tmp;
    	}
    	return NULL;
    }
    
    void StampaAlbero(treenode* radice)
    {
    	if(radice!=NULL)
    	{
    		cout<<"\t"<<radice->info;
    		StampaAlbero(radice->sx);
    		StampaAlbero(radice->dx);
    	}
    }
    E i 2 errori, entrambi alla riga
    codice:
    void BilanciaAlbero(treenode* &radice)
    {
    sono:

    (94) : error C2556: 'void __cdecl BilanciaAlbero(struct treenode *& )' : overloaded function differs only by return type from 'struct treenode *__cdecl BilanciaAlbero(struct treenode *& )'
    P:\Programmi\Visual C++\MyProjects\Alberi3\sorgente.cpp(11) : see declaration of 'BilanciaAlbero'
    (94) : error C2040: 'BilanciaAlbero' : 'void (struct treenode *& )' differs in levels of indirection from 'struct treenode *(struct treenode *& )'

    Arabo... maledetto programma
    Debian GNU/Linux sid
    Publishing a theory should not be the end of one's conversation with the universe, but the beginning. (Eric S. Raymond)
    Kernel 2.6.14-ck1

  10. #10
    Utente di HTML.it
    Registrato dal
    Feb 2002
    Messaggi
    1,202
    Originariamente inviato da PunkIvi
    Eccolo l'errore:

    tmp->info=array[mediana];

    Non puoi mettere un array in un int!

    mediana è un intero
    Debian GNU/Linux sid
    Publishing a theory should not be the end of one's conversation with the universe, but the beginning. (Eric S. Raymond)
    Kernel 2.6.14-ck1

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.