Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1319 connectés 

  FORUM HardWare.fr
  Programmation
  C++

  probleme operateur matrice [i][j]

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

probleme operateur matrice [i][j]

n°1255876
pocou64
Posté le 30-11-2005 à 10:36:10  profilanswer
 

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 :
  1. #include <iostream>
  2. #include <fstream>
  3. #include "vector.h"
  4. template<class T> class Matrix : public Vector< Vector<T> >{
  5. private:
  6.  int nrows;   // number of rows of the matrix
  7.  int ncols;             // number of columns of the matrix
  8. public:
  9.  Matrix(); // default constructor, uses default constructor for v
  10.   Matrix(int Nrows, int Ncols);  // alternate constructor
  11.     //T& operator() (int i, int j) const; //  function call overload (-,-)
  12.  //T& operator[][] (unsigned  i),(unsigned  j) const; //  function call overload [i][j]
  13.  friend Matrix<T> operator*(const Matrix<T>& m1, const Matrix<T>& m2);// overload * for matrix multiplication
  14.  friend std::istream& operator>>(std::istream& is, Matrix<T>& m);// keyboard input
  15.  ..............
  16. };
  17. // default constructor, using default constructor for v
  18. template<class T>
  19. Matrix<T>::Matrix() : Vector< Vector<T> >(0,Vector<T>(0)),nrows(0), ncols(0)
  20. {
  21. }
  22. // alternate constructor
  23. template<class T>
  24. Matrix<T>::Matrix(int Nrows, int Ncols) : Vector< Vector<T> >(Nrows,Vector<T>(Ncols)),nrows(Nrows), ncols(Ncols)
  25. {
  26.  std::cout << "vecteur cree de taille = " << Vector< <Vector<T> >::GetNum() << "\n";
  27. }
  28. //  function call overload [i][j]
  29. template<class T>
  30. T& Matrix<T>::operator[][] (unsigned  i),(unsigned  j) const
  31. {
  32. int numv;
  33. if (i < 0)
  34.        i = 0;  // Range error causes index to equal 0
  35.   // should really throw an exception
  36. if (j < 0)
  37.        j = 0;  // Range error causes index to equal 0
  38.   // should really throw an exception
  39. numv=j+(nrows)*(i);
  40. return pdata[numv];
  41. }
  42. // for matrix multiplication
  43. template<class T>
  44. Matrix<T> operator*(const Matrix<T>& m1, const Matrix<T>& m2)
  45. {
  46. std::cout <<" Multiplication Matrix...\n";
  47. int i,j,k;
  48. Matrix<T> temp(m1.nrows,m2.ncols);
  49. for (i=0;i<m1.nrows;i++){
  50.  for (j=0;j<m1.ncols;j++)
  51.  {
  52.   for (k=0;k<m2.nrows;k++)
  53.   {
  54.    if(k==0) temp[i][j]=(m1[i][k]*m2[k][j]);
  55.    else temp[i][j]= temp[i][j] + (m1[i][k]*m2[k][j]);
  56.   }
  57.  }
  58. }
  59. return temp;
  60. }
  61. // keyboard input
  62. template<class T>
  63. std::istream& operator>>(std::istream& is, Matrix<T>& m) {
  64. int Nrows, Ncols;
  65. // input the size of the matrix
  66. std::cout << "input num of rows \n";
  67.     is >> Nrows;
  68. std::cout << "input num of columns \n";
  69.     is >> Ncols;
  70.    
  71. // create a temporary matrix of the correct size
  72.     Matrix<T> temp(Nrows, Ncols);
  73. std::cout << "input the matrix elements\n";
  74. for (int i=0; i<Nrows; i++)
  75. {
  76.  for (int j=0 ; j<Ncols; j++)
  77.  {
  78.   //numv=j+(Nrows)*(i);
  79.   std::cout << "element [" << i << "][" << j << "] :";
  80.   is >> temp.pdata[i][j];
  81.  }
  82. }
  83.     m = temp;
  84. return is;
  85. }


 
et maintenant une partie de ma classe vector.h
 

Code :
  1. #include <iostream>
  2. #include <fstream>
  3. template<class  T> class Vector {
  4. private:
  5. int num; // Number of elements
  6.  
  7. void Init(int Num); // private function since user should not call it
  8.     // only for the member functions to call
  9. protected:
  10. T* pdata; // Pointer to the data
  11. public:
  12. Vector(); // default constructor
  13.     Vector(int Num); // alternate constructor
  14.     Vector(const Vector<T>& v); // copy constructor
  15. Vector(int Num, Vector<T> v);
  16.     ~Vector(); // destructor
  17. int GetNum() const; // access function
  18.     Vector<T>& operator= (const Vector<T>& v); //  overloaded assignment operator
  19.     T& operator[] (int i) const; // overloaded array access operator
  20.              ..........
  21. };
  22. // default constructor
  23. template<class T>
  24. Vector<T>::Vector() : num(0), pdata(0) {}
  25. // alternate constructor  
  26. template<class T>
  27. Vector<T>::Vector(int Num)
  28. {
  29. Init(Num);
  30. }
  31. // copy constructor  
  32. template<class T>
  33. Vector<T>::Vector(const Vector<T>& copy) {
  34. Init(copy.GetNum()); // allocate the memory  
  35. // copy the data members
  36. if (pdata) for (int i=0; i<num; i++) pdata[i]=copy.pdata[i];
  37. }
  38. // rien
  39. template<class T>
  40. Vector<T>::Vector(int Num, Vector<T> v) {
  41. Init(Num);
  42. }
  43. // destructor
  44. template<class T>
  45. Vector<T>::~Vector()
  46. {
  47.     delete [] pdata; // free the dynamic memory  
  48. }
  49. // initialise data, called by the constructors
  50. template<class T>
  51. void Vector<T>::Init(int Num)
  52. {
  53. num = Num;
  54.    if (num <= 0)
  55.        pdata = 0;  // Object construction failed!
  56.     else
  57.     pdata = new T[num];  // Allocate memory for vector
  58. }
  59. // assignment operator  
  60. template<class T>
  61. Vector<T>& Vector<T>::operator=(const Vector<T>& copy)
  62. {
  63. if (this == &copy) return *this; // Can't copy self to self (that is v = v  
  64. // in main is dealt with)
  65.     delete [] pdata; // delete existing memory
  66.     Init(copy.GetNum()); // create new memory then copy data
  67.    if (pdata) for (int i=0; i<copy.GetNum(); i++) pdata[i] = copy.pdata[i];
  68.    return *this;
  69. }
  70. // array access operator
  71. template<class T>
  72. T& Vector<T>::operator[](int i) const
  73. {
  74.     if (i < 0)
  75.        i = 0;  // Range error causes index to equal 0
  76.   // should really throw an exception
  77.     return pdata[i];
  78. }
  79. // return the size of the vector
  80. template<class T>
  81. int Vector<T>::GetNum() const
  82. {
  83.    return num;
  84. }

mood
Publicité
Posté le 30-11-2005 à 10:36:10  profilanswer
 

n°1255987
Taz
bisounours-codeur
Posté le 30-11-2005 à 14:08:24  profilanswer
 

beurk. déjà utilise std::vector<> dès que possible.
 
ensuite, il faut créer un proxy, exemple vite torché :
 

Code :
  1. #include <vector>
  2. template<typename T>
  3. class Matrix
  4. {
  5.   typedef std::vector<T> Row;
  6.   std::vector<Row> rows;
  7. public:
  8.   Matrix(size_t n, size_t m)
  9.     : rows(n, Row(m))
  10.   { }
  11.   struct MatrixProxy
  12.   {
  13.     Row &row;
  14.     MatrixProxy(Row &a_row)
  15.       : row(a_row)
  16.     { }
  17.     T& operator[](size_t j)
  18.     {
  19.       return this->row[j];
  20.     }
  21.   };
  22.   MatrixProxy operator[](size_t i)
  23.   {
  24.     return this->rows[i];
  25.   }
  26. };
  27. int main()
  28. {
  29.   Matrix<float> m(3, 4);
  30.   m[1][2] = m[2][1] + 1.0f;
  31. }

n°1256005
pocou64
Posté le 30-11-2005 à 14:26:47  profilanswer
 

merci de ta reponse Taz mais j'ai pour consigne d'avoir la classe matrix qui herite de la classe vector et donc d'utiliser :

Code :
  1. template<typename T>
  2. class Matrix : public Vector<Vector<T> {....


c'est d'ailleurs la seul consigne que j'ai.

n°1256060
Taz
bisounours-codeur
Posté le 30-11-2005 à 15:25:30  profilanswer
 
n°1256190
pocou64
Posté le 30-11-2005 à 17:19:05  profilanswer
 

Taz a écrit :

et alors ...


 
Dans l'exemple que tu m'a donne, et d'apres ma tres jeune experience en c++, tu n'utilises pas l'heritage et je n'arrive pas a l'apppliquer a mon cas.
De plus le compilateur n'aime pas l'instruction std::vector, il dis:

Code :
  1. y:\public\part22_ 3 heritage complex template\matrix.h(5): error C2039: 'Vector' : is not a member of 'std'

n°1256466
Joel F
Real men use unique_ptr
Posté le 30-11-2005 à 22:28:26  profilanswer
 

vector pas Vector ...

n°1256635
pocou64
Posté le 01-12-2005 à 10:11:40  profilanswer
 

si je met vector au lieu de Vector plus rien n'est reconnu puisque ma classe Vector est defini avec la majuscule, par contre effectivement j'ai oublie le V dans mon include.  
Si tu vois autre chose qui pourrait poser probleme n'hesites pas.

n°1256733
Taz
bisounours-codeur
Posté le 01-12-2005 à 12:27:54  profilanswer
 

bah on voit rien : mets toi au boulot, t'as rien à faire, juste en héritant, t'as le résultat que tu veux.


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C++

  probleme operateur matrice [i][j]

 

Sujets relatifs
Problème de submit sous IE[php] Programmation objet, probleme d'inclusion
j'ai un probleme avec un exo en matlabExécution à distance, problème de répertoire
Problème de bordure en HTMLProblème de Copie (StreamReader)
[CSS] Problème float IE Mac[Resolu] Probleme Register_Globals
Probleme declaration fonctionProblème avec la commande ISDATE
Plus de sujets relatifs à : probleme operateur matrice [i][j]


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR