1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
   |  
// #include blablabla...
 
template<class T>
class MATRIX
{
private:
    size_t nl;
    size_t nc;
    T** tab;
 
public:
    //Constructors
 
    MATRIX();
 
    MATRIX(const size_t& li,const size_t& co,const T& val=T());
 
    template<class U>
    MATRIX(const MATRIX<U>& mat); // je voudrais etre capable d'accepter des matrices d'un template à l'autre (ex dans le main.cpp).
 
    //Destructor
    ~MATRIX(){ Reset();}
 
    // Operators
    template<class U>
    MATRIX<T>& operator=(const MATRIX<U>& mat);  // idem mais ça ne marche pas
    T& operator()(const size_t& i, const size_t& j);
    const T& operator()(const size_t& i,const size_t& j) const;    
    // les autres operatoeurs +*/-... sont pas encores implementes
 
 
    template<class U>
    friend std::ostream &operator<<(std::ostream &out,MATRIX<U> &mat);
    void Print(std::ostream& out) const;
 
    inline const size_t& Line() const{return nl;}
    inline const size_t& Column() const{return nc;}
 
    bool Allocate(const size_t& li,const size_t& co);
    void Reset();
 
    template<class U>
    void Initialize(const U& val);
 
    MATRIX<T>& Transpose(); 
}
 
...
 
template<class T>
template<class U>
MATRIX<T>::MATRIX(const MATRIX<U>& mat)
{
    tab=(T**)NULL;
 
    if(!Allocate(mat.Line(),mat.Column()))
        std::cout<<"Can not allocate memory!"<<std::endl;
 
    std::cout<<"Dans le constructeur de recopie"<<std::endl;
    for(size_t i=0;i<nl;++i)
        for(size_t j=0;j<nc;++j)
            tab[i][j]=(T)(mat(i,j));
}
 
...
 
template<class T>
    template<class U>
MATRIX<T>& MATRIX<T>::operator=(const MATRIX<U>& mat)
{
    if(this!=&mat) // plante à la compilation (voir main.cpp)
    {
        Reset();
        Allocate(mat.Line(),mat.Column());
 
        for(size_t i=0;i<nl;++i)
            for(size_t j=0;j<nc;++j)
                tab[i][j]=static_cast<T> (mat(i,j));
    }
 
    return *this;
}
 
...
 
template<class T>
MATRIX<T>& MATRIX<T>::Transpose()
{
    MATRIX<T> tmp(*this);  // ICI l'operateur de recopie n'est pas appelé ??
 
    Reset();
    Allocate(tmp.Column(),tmp.Line());
 
    for(size_t i=0;i<nl;++i)
        for(size_t j=0;j<nc;++j)
            tab[i][j]=tmp(j,i);
 
    return *this;
} | 
Partager