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
l' operatore ridefinito fuori dal corpo e` cosi` dichiarato :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 &) ; };
e l'errore che ottengo e` il seguente :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; }
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

Rispondi quotando