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

    C++ dubbio su template typename

    Salve a tutti
    volevo chiedere delucidazioni in merito a un errore che ottengo quando specifico il tipo all interno delle parentesi <> di una dichiarazione di una funzione friend di un template di classe ...

    posto un po di codice che e` meglio . ..
    codice:
    template<typename mType>
    Matrix<mType> operator+(const Matrix<mType>& ,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>& );   
    	friend Matrix<mType> operator* <mType>(const Matrix<mType>& ,const Matrix<mType>& );
    
    	public:
    	inline Matrix(unsigned int = 3, unsigned int = 3 );    // 3x3 as defaulf matrix
    	inline Matrix(unsigned int, unsigned int, mType );     // constructor with initialization
    	inline Matrix( const Matrix<mType>& );  			   // copy constructor 
    	~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 );
    	void trasp();
    	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>& );   
    	inline void setEmpty(bool t){ empty_ = t ; }
    	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 &) ;
    
    };
    i problemi sono (o meglio le perplessita`) riguardano l'overload degli operatori + e * .. in particolare nell' operatore + se metto il tipo <mType> ricevo un errore :
    codice:
    n file included from /usr/include/c++/4.4/bits/stl_algobase.h:67,
                     from /usr/include/c++/4.4/bits/char_traits.h:41,
                     from /usr/include/c++/4.4/ios:41,
                     from /usr/include/c++/4.4/ostream:40,
                     from /usr/include/c++/4.4/iostream:40,
                     from main.C:1:
    /usr/include/c++/4.4/bits/stl_iterator_base_types.h: In instantiation of ‘std::iterator_traits<double>’:
    /usr/include/c++/4.4/bits/stl_iterator.h:96:   instantiated from ‘std::reverse_iterator<double>’
    matrix.H:35:   instantiated from ‘Matrix<double>’
    main.C:24:   instantiated from here
    /usr/include/c++/4.4/bits/stl_iterator_base_types.h:127: error: ‘double’ is not a class, struct, or union type
    /usr/include/c++/4.4/bits/stl_iterator_base_types.h:128: error: ‘double’ is not a class, struct, or union type
    /usr/include/c++/4.4/bits/stl_iterator_base_types.h:129: error: ‘double’ is not a class, struct, or union type
    /usr/include/c++/4.4/bits/stl_iterator_base_types.h:130: error: ‘double’ is not a class, struct, or union type
    /usr/include/c++/4.4/bits/stl_iterator_base_types.h:131: error: ‘double’ is not a class, struct, or union type
    che non ottengo invece nel caso dell'operatore * pur specificando il tipo come potete vedere ..

    ovviamente nelle definizioni delle funzioni (come nelle foward declararation) ovviamente non uso le parentesi <> .. qualcuno mi aiuta a capire il perche ?
    C
    C Saluti .. Il DrudoX
    C
    STOP
    END

  2. #2
    Utente bannato
    Registrato dal
    Apr 2012
    Messaggi
    510
    codice:
    template <class mType2>
    friend Matrix<mType> operator+ ( const Matrix<mType2>& , const Matrix<mType2>& );  
    template <class mType2> 
    friend Matrix<mType2> operator* (const Matrix<mType2>& ,const Matrix<mType2>& );

  3. #3
    Utente di HTML.it L'avatar di drudox
    Registrato dal
    Sep 2011
    Messaggi
    93
    si grazie .. ma potresti darmi anche una spiegazione ?
    C
    C Saluti .. Il DrudoX
    C
    STOP
    END

  4. #4
    Utente di HTML.it L'avatar di drudox
    Registrato dal
    Sep 2011
    Messaggi
    93
    a questo punto lo stesso credo valga per gli operator<< giusto ?
    C
    C Saluti .. Il DrudoX
    C
    STOP
    END

  5. #5
    La dichiarazione friend è una dichiarazione in un certo senso slegata dal resto della classe, dato che si riferisce a roba che sta fuori da essa; per questo motivo, non è implicito il "template<typename mType>", ovvero che la funzione che stai rendendo friend sia un template, perché questo è implicito solo per i membri della classe. Per questo motivo, dunque, la dichiarazione friend che ti serve necessita del template, dato che stai rendendo friend un template che in linea di principio è indipendente da quello della classe.

    In alternativa, credo che sarebbe bastato scrivere:
    codice:
    	friend std::ostream&  operator<< (std::ostream& , Matrix<mType>& );
    	friend std::ostream&  operator<< (std::ostream& , const  Matrix<mType>& );
    ma dovrei controllare sullo standard.
    Amaro C++, il gusto pieno dell'undefined behavior.

  6. #6
    Utente di HTML.it L'avatar di drudox
    Registrato dal
    Sep 2011
    Messaggi
    93
    ah ok !! ma in questo modo (operatori apparte ovviamente) come e` possibile specificare che solo le istanze dello stesso tipo devono essete friend ? mi spiego ... supponiamo che venga istanziato un template di classe Matrix come double.. come specificare che solo le funzioni template friend <double> devono esser friend ?
    C
    C Saluti .. Il DrudoX
    C
    STOP
    END

  7. #7
    Credo che basti la seconda forma che ti ho detto (dove mType è preso dal template della classe), ma comunque è irrilevante, dato che le altre versioni del template degli operatori non possono essere chiamate su oggetti Matrix basati su un tipo diverso.
    Amaro C++, il gusto pieno dell'undefined behavior.

  8. #8
    Utente di HTML.it L'avatar di drudox
    Registrato dal
    Sep 2011
    Messaggi
    93
    si si infatti ho detto operatori a parte
    C
    C Saluti .. Il DrudoX
    C
    STOP
    END

  9. #9
    ovvero che la funzione che stai rendendo friend sia un template, perché questo è implicito solo per i membri della classe.[im*g]http://www.###########/g.gif[/img]

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.