Pagina 1 di 3 1 2 3 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 27
  1. #1
    Utente di HTML.it L'avatar di drudox
    Registrato dal
    Sep 2011
    Messaggi
    93

    [C++] problema overloading operatore classe template

    Salve a tutti .. vediamo se qualcuno riesce a darmi una mano .. non riesco a capire la violazione di memoria che ottengo alla chiamata al distruttore di un obj al quale e` stata assegnato il risultato di un operazione di overloading del operatore +



    codice:
      
    
    #ifndef ARRAY_H
    #define ARRAY_H
    
    #include<iostream>
    #include<new>         // eception for bad_alloc
    #include<cstdlib>
    
    using namespace std;
    
    // forward declaration
    template<typename T> 
    class Array ;
    
    template<typename T>
    ostream& operator<<( ostream& output, const Array<T>& A) ;
    
    
    
    template<typename T>
    class Array{
    
    	friend ostream& operator<< <T>( ostream& , const Array<T>& );		  
    	
    
    
    	public:
    	Array(int = 1, T = 0);
    	Array( const Array<T> & );
    	~Array();
    //	T getElement()const;
    //	void setElement(int , T);
    //	void print()const;
    
    	const Array &operator+=( const Array &);
    	const Array operator+( const Array &) const;
    
    	private:
    	
    	int size;
    	T* arrayPtr; 
    	// utility function
    	T* allocate( const T& )throw(bad_alloc);
    	T* allocate( const Array<T>& )throw(bad_alloc);
    
    };
    
    
    template<typename T>
    Array<T>::Array(int element , T value)
    	:size(element),
    	 arrayPtr(allocate(value)) 
    { }
    			
    	
    		  
    template<typename T>
    Array<T>::Array( const Array<T> & A)
    	: size(A.size),
    	  arrayPtr(allocate(A)) 	  
    { }
    
    
    
    		  
    template<typename T>
    T* Array<T>::allocate( const T&value ) throw(bad_alloc)
    {
    	T* aPtr ;	  
    	try
    	{
    		aPtr = new T[size];
    		// initialization part
    		for(int i=0 ; i< size ; i++)
    			{
    			  aPtr[i] = value;
    			}
    	}
    	catch( bad_alloc & unnalocate )
    	{
    		cout << unnalocate.what();	
    	}
    	return aPtr ;
    }
    
    
    
    template<typename T>
    T* Array<T>::allocate( const Array<T>& ToCopy ) throw(bad_alloc)
    {
    	T* aPtr ;	  
    	try
    	{
    		aPtr = new T[size];
    		// initialization part
    		for(int i=0 ; i< size ; i++)
    			{
    			  aPtr[i] = ToCopy.arrayPtr[i] ;
    			}
    	}
    	catch( bad_alloc & unnalocate )
    	{
    		cout << unnalocate.what();	
    	}
    	return aPtr ;
    }
    
    
    
    template<typename T >
    Array<T>::~Array()
    {
      cout << "distructor called for array" << endl;	
      cout << *this ;
      delete[] arrayPtr ;
    }
    
    template<typename T>
    ostream& operator<<( ostream& output, const Array<T>& A)
    { 
    	for(int i= 0 ; i < A.size ; i++ )		  
    		output << A.arrayPtr[i] << " " ;
    	
    	cout << endl;
    }
    
    template<typename T>
    const Array<T>& Array<T>::operator+=( const Array<T> & B)
    {
    		if( B.size != size )
    		{
    				cout << "Vector must be to same size" << endl;
    				exit(1);
    		}
    		else
    		{
    				for(int i=0 ; i< size ; i++)
    				{
    					arrayPtr[i] += B.arrayPtr[i] ;			
    				}
    			return *this ;
    		}
    }
    
    
    
    template<typename T>
    const Array<T> Array<T>::operator+( const Array &A) const
    {
    		if( size != A.size)
    		{	cout << "Vector must be to same size" << endl;
    				exit(1);
    		}
    		else
    		{
    				Array<T> Temp(A.size) ;
    				for(int i=0;i<size;i++)
    						Temp.arrayPtr[i] = arrayPtr[i] + A.arrayPtr[i]; 
    
    				return Temp;
    		}
    
    }
    
    
    
    
    #endif
    e questo il programma di prova



    codice:
    #include<iostream>
    
    #include "array.h"
    
    using namespace std;
    
    int main()
    {	
    	Array<double> A(5,2.2);	  
    	
    	Array<double> B(5,1.1); 
    
    	cout << A << endl ;
    
    	cout << B << endl ;
    	
    	Array<double> C(5);
    	C=  A+=B ;
    	cout << C << endl ; 
    
    		 
    	
    	Array<double> D(B);
    	D = C + B;	
    	cout << "ok " << endl ;
    	cout << D << endl;
    	
    
    
    
    
    	return 0;
    }
    C
    C Saluti .. Il DrudoX
    C
    STOP
    END

  2. #2
    Prova ad implementare l'operatore di copia, una cosa del tipo:

    codice:
    Array& Array::operator=(const Array &rhs)
    {
        if(this != &rhs)
        {
            delete [] arrayPtr;
            allocate(rhs);
        }
    
        return *this;
    }
    Fracty - The Fractal Generator



    If you cannot choose a concise name that expresses what the method does, it is possible that your method is attempting to perform too many diverse tasks.

  3. #3
    Utente di HTML.it L'avatar di drudox
    Registrato dal
    Sep 2011
    Messaggi
    93
    Grazie per l'interessamento ..
    Effettivamente appena mi hai dato il suggerimento ho pensato anche io potesse dipendere dalla mancata ridevinizione operatore =

    ma a questo punto .. implementato come mi hai detto compila ma va in segmentation fault ...
    C
    C Saluti .. Il DrudoX
    C
    STOP
    END

  4. #4
    L'ho appena provato, e a parte l'errore sul metodo operator<<, che non restituisce niente quando invece dovrebbe, non mi da nessun problema quando viene eseguito...
    Fracty - The Fractal Generator



    If you cannot choose a concise name that expresses what the method does, it is possible that your method is attempting to perform too many diverse tasks.

  5. #5
    Utente di HTML.it L'avatar di drudox
    Registrato dal
    Sep 2011
    Messaggi
    93
    ma hai eseguito il mio codice piu` una definizione di funzione membro in questo modo :

    codice:
    template<typename T>
    Array<T>& Array<T>::operator=(const Array &A)
    {
        if(this != &A)
        {
            delete [] arrayPtr;
            allocate(A);
        }
    
        return *this;
    }
    C
    C Saluti .. Il DrudoX
    C
    STOP
    END

  6. #6
    un copia e incolla, a parte quell'errore che ti ho detto e ho corretto e il fatto che ho implementato il metodo all'interno della classe.
    Fracty - The Fractal Generator



    If you cannot choose a concise name that expresses what the method does, it is possible that your method is attempting to perform too many diverse tasks.

  7. #7
    Utente di HTML.it L'avatar di drudox
    Registrato dal
    Sep 2011
    Messaggi
    93
    Io l'ho implementato fuori come ho scritto .. quando chiama l'operatore = ( ho visto con il debug ) l'argomento passato sembra non avere un arrayPtr allocato ..
    C
    C Saluti .. Il DrudoX
    C
    STOP
    END

  8. #8
    Utente di HTML.it L'avatar di drudox
    Registrato dal
    Sep 2011
    Messaggi
    93
    che compilatore usi ?? io gcc 4.2.4

    e` strano che mi dia segmentation fault .. inoltre a me l'operatore << non da alcun problema
    C
    C Saluti .. Il DrudoX
    C
    STOP
    END

  9. #9
    Originariamente inviato da drudox
    Io l'ho implementato fuori come ho scritto .. quando chiama l'operatore = ( ho visto con il debug ) l'argomento passato sembra non avere un arrayPtr allocato ..
    Ma la memoria che rilasci di arrayPtr non appartiene al parametro passato come argomento, bensì all'oggetto stesso in cui vuoi copiarne i dati. Esattamente dove va in segmentation fault?
    Fracty - The Fractal Generator



    If you cannot choose a concise name that expresses what the method does, it is possible that your method is attempting to perform too many diverse tasks.

  10. #10
    Utente di HTML.it L'avatar di drudox
    Registrato dal
    Sep 2011
    Messaggi
    93
    esattamente quando chiama il costruttore di copia .. qui .. il toCopy che passa e` nullo
    codice:
    aPtr[i] = ToCopy.arrayPtr[i] ;
    C
    C Saluti .. Il DrudoX
    C
    STOP
    END

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 © 2024 vBulletin Solutions, Inc. All rights reserved.