pocou64 | bonjour a tous, je m'adresse a vous car je n'arrive pas a sortir de mon probleme...
j'ai une class matrix qui est une template, qui herite d'une classe vector (qui elle aussi est une template), pour l'instant je me balade dans ma matrice en utilisant m(i,j) et je voudrais utiliser m[i][j].
j'ai commence une solution mais je ne m'en sort pas, voir le code ci joint (non complet mais suffisant je pense pour comprendre mon probleme) de matrix.h
merci de votre aide...
Code :
- #include <iostream>
- #include <fstream>
- #include "vector.h"
- template<class T> class Matrix : public Vector< Vector<T> >{
- private:
- int nrows; // number of rows of the matrix
- int ncols; // number of columns of the matrix
- public:
- Matrix(); // default constructor, uses default constructor for v
- Matrix(int Nrows, int Ncols); // alternate constructor
- //T& operator() (int i, int j) const; // function call overload (-,-)
- //T& operator[][] (unsigned i),(unsigned j) const; // function call overload [i][j]
- friend Matrix<T> operator*(const Matrix<T>& m1, const Matrix<T>& m2);// overload * for matrix multiplication
- friend std::istream& operator>>(std::istream& is, Matrix<T>& m);// keyboard input
- ..............
- };
- // default constructor, using default constructor for v
- template<class T>
- Matrix<T>::Matrix() : Vector< Vector<T> >(0,Vector<T>(0)),nrows(0), ncols(0)
- {
- }
- // alternate constructor
- template<class T>
- Matrix<T>::Matrix(int Nrows, int Ncols) : Vector< Vector<T> >(Nrows,Vector<T>(Ncols)),nrows(Nrows), ncols(Ncols)
- {
- std::cout << "vecteur cree de taille = " << Vector< <Vector<T> >::GetNum() << "\n";
- }
- // function call overload [i][j]
- template<class T>
- T& Matrix<T>::operator[][] (unsigned i),(unsigned j) const
- {
- int numv;
- if (i < 0)
- i = 0; // Range error causes index to equal 0
- // should really throw an exception
- if (j < 0)
- j = 0; // Range error causes index to equal 0
- // should really throw an exception
- numv=j+(nrows)*(i);
- return pdata[numv];
- }
- // for matrix multiplication
- template<class T>
- Matrix<T> operator*(const Matrix<T>& m1, const Matrix<T>& m2)
- {
- std::cout <<" Multiplication Matrix...\n";
- int i,j,k;
- Matrix<T> temp(m1.nrows,m2.ncols);
- for (i=0;i<m1.nrows;i++){
- for (j=0;j<m1.ncols;j++)
- {
- for (k=0;k<m2.nrows;k++)
- {
- if(k==0) temp[i][j]=(m1[i][k]*m2[k][j]);
- else temp[i][j]= temp[i][j] + (m1[i][k]*m2[k][j]);
- }
- }
- }
- return temp;
- }
- // keyboard input
- template<class T>
- std::istream& operator>>(std::istream& is, Matrix<T>& m) {
- int Nrows, Ncols;
- // input the size of the matrix
- std::cout << "input num of rows \n";
- is >> Nrows;
- std::cout << "input num of columns \n";
- is >> Ncols;
-
- // create a temporary matrix of the correct size
- Matrix<T> temp(Nrows, Ncols);
- std::cout << "input the matrix elements\n";
- for (int i=0; i<Nrows; i++)
- {
- for (int j=0 ; j<Ncols; j++)
- {
- //numv=j+(Nrows)*(i);
- std::cout << "element [" << i << "][" << j << "] :";
- is >> temp.pdata[i][j];
- }
- }
- m = temp;
- return is;
- }
|
et maintenant une partie de ma classe vector.h
|