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

    [C++] problemi overloading operatore in classe template

    Buona sera a tutti .. vorrei fare l'overloading dell' operatore ^ (con lo scopo di effettuare l'elevamento a potenza degli elementi della matrice definita come template .. )

    ora io vorrei usare un qualsiasi tipo float , double , int .. etc .. come argomento dell operatore .. cosi` ho pensato di definirlo come template interno alla classe .. ma non sempra funzionare .. mi spiego :

    la definizione della classe e` questa

    codice:
    template<typename mType> class Matrix;
    
    template<typename mType> 
    std::ostream& operator<< (std::ostream& , Matrix<mType> &);  
    
    template<typename mType> 
    std::ostream& operator<< (std::ostream& , const Matrix<mType> &);  
    
    template<typename mType>
    Matrix<mType> operator+(const Matrix<mType>& ,const Matrix<mType>&); 
    
    template<class mType>
    class Matrix
    {
    	friend std::ostream&  operator<< <mType>(std::ostream& , Matrix<mType>& );
    	friend std::ostream&  operator<< <mType>(std::ostream& , const  Matrix<mType>& );
    	friend Matrix<mType> operator+ <>( const Matrix<mType>& , const Matrix<mType>& );   
    
    	public:
    	inline Matrix(unsigned int = 3, unsigned int = 3 );    // 3x3 as defaulf matrix
    	Matrix(unsigned int, unsigned int, mType );     // constructor with initialization
    	~Matrix();
    	inline bool isEmpty()const { return empty_ ; }
    	inline bool allocate( unsigned int , unsigned int );
    	void print( unsigned int = 5 , unsigned int = 3)const ;	 //	 weight and precision as defaul .. 
    	void Rand();
    	void Rand(unsigned int , unsigned int );
    	inline unsigned int getRowSize()const{ return row_ ;}
    	inline unsigned int getColumnSize()const{ return column_ ;}
    	inline vector<unsigned int> size()const;
    	inline mType& operator()(unsigned int , unsigned int )const;
    #   include "proxy.H"
    	inline Proxy operator[]( unsigned int i ){ return Proxy(*this,i); }
    	Matrix<mType>& operator=( const Matrix<mType>& );   
    	
    	template<typename T> Matrix<mType>& operator^ (const T);   
    	bool operator==(const Matrix<mType>& );
    
    	private:
    	unsigned int row_ 	;
    	unsigned int column_;		
    	mType ** mPtr_ 	;
    	
    //	utility function 	
    	bool isAllocated_;
    	bool empty_;
    	inline bool allocate_();
    	bool initialize_( mType &) ;
    
    };
    l' operatore ridefinito fuori dal corpo e` cosi` dichiarato :


    codice:
    template<typename mType, typename T>
    Matrix<mType>& Matrix<mType>::template operator^(const T exp)
    {
    		for( unsigned int i=0; i< row_ ; i++ )
    			for( unsigned int j=0; j< column_ ; j++ )
    				this->mPtr_[i][j] = pow (mPtr_[i][j], exp);
    		return *this;
    }
    e l'errore che ottengo e` il seguente :

    codice:
    In file included from main.C:6:
    matrix.H:302: error: prototype for ‘Matrix<mType>& Matrix<mType>::operator^(T)’ does not match any in class ‘Matrix<mType>’
    matrix.H:51: error: candidate is: template<class mType> template<class T> Matrix& Matrix::operator^(T)

    non so se la definizione di template vada bene .. help me
    grazie in anticipo
    C
    C Saluti .. Il DrudoX
    C
    STOP
    END

  2. #2
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381

    Re: [C++] problemi overloading operatore in classe template

    template<typename mType, typename T>
    non fa parte della definizione di Matrix.

    Una funzione member template richiede il proprio parametro specificato a parte.

    codice:
    template<typename mType>
    template<typename T>
    Matrix<mType>& Matrix<mType>::template operator^(const T exp)
    {
    		for( unsigned int i=0; i< row_ ; i++ )
    			for( unsigned int j=0; j< column_ ; j++ )
    				this->mPtr_[i][j] = pow (mPtr_[i][j], exp);
    		return *this;
    }
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  3. #3
    Utente di HTML.it L'avatar di drudox
    Registrato dal
    Sep 2011
    Messaggi
    93
    perfettamente ragione .. grazie Risolto
    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.