• Lunes 6 de Mayo de 2024, 02:51

Autor Tema:  Clase matriz con plantillas  (Leído 2253 veces)

antlcn

  • Nuevo Miembro
  • *
  • Mensajes: 12
    • Ver Perfil
Clase matriz con plantillas
« en: Viernes 5 de Agosto de 2011, 17:44 »
0
Hola, tengo un programa escrito en C++ que define una clase matriz con plantillas esta todo en un .h con las funciones inline, y al compilar me da errores que no se de que son. Una ayudita por favor!.

Código: C++
  1. #ifndef _Matrix_H_
  2. #define _Matrix_H_
  3. #include <cassert>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7. namespace storage{
  8.     template<class Type>
  9.         class Matrix
  10.         {
  11.             public:
  12.                 /* constructor vacio
  13.                  */
  14.                 inline Matrix();
  15.                 /* constructor copia
  16.                  */
  17.                 inline Matrix(const Matrix &M);
  18.                 /* constructor parametrizado
  19.                  */
  20.                 inline Matrix(unsigned int nRows, unsigned int nCols);
  21.                 /* destructor
  22.                  */
  23.                 inline ~Matrix();
  24.                 /* redimensionar la matriz
  25.                  */            
  26.                 inline void resize(unsigned int nRows, unsigned int nCols);
  27.                 /* indica el numero de filas
  28.                 */
  29.                 unsigned int getNRows() { return _nRows;}
  30.                 /* indica el numero de columnas
  31.                  */
  32.                 unsigned int getNCols() { return _nCols;}
  33.                 /* retorna el valor indicado por las variables
  34.                  */
  35.                 inline Type & get(unsigned int r,unsigned int c);
  36.                 /* hace que la matriz sea la indentidad
  37.                  */
  38.                 inline void setIdentity();
  39.                 /* operador de asignacion
  40.                  */
  41.                 inline Matrix & operator=(const Matrix &M);
  42.                 /* realiza la suma de dos matrices
  43.                  */
  44.                 inline Matrix operator+(const Matrix &M);
  45.                
  46.                
  47.         private:
  48.             Type **_data;
  49.             unsigned int _nRows;
  50.             unsigned int _nCols;
  51.         };
  52.  
  53.        
  54.  
  55. //////////////////////////
  56. //
  57. //////////////////////////
  58. template<class Type>
  59. Matrix<Type>::Matrix()
  60. {
  61.   _data=NULL;
  62.   _nRows=0;
  63.   _nCols=0;
  64. }
  65.  
  66. //////////////////////////
  67. //
  68. //////////////////////////
  69. template<class Type>
  70. Matrix<Type>::Matrix(const Matrix &M)
  71. {
  72.   int i=0,j=0;
  73.   _data=NULL;
  74.   resize(M._nRows, M._nCols);
  75.  
  76.   for(i=0;i<_nRows;i++)
  77.   {
  78.     for(j=0;j<_nCols;j++)
  79.     {
  80.       _data[i][j]=M._data[i][j];
  81.     }
  82.   }
  83. }
  84.  
  85. //////////////////////////
  86. //
  87. //////////////////////////
  88. template<class Type>
  89. Matrix<Type>::Matrix(unsigned int nRows, unsigned int nCols)
  90. {
  91.   _data=NULL;
  92.   resize(nRows, nCols);
  93. }
  94.  
  95.  
  96. //////////////////////////
  97. //
  98. //////////////////////////
  99. template<class Type>
  100. Matrix<Type>::~Matrix()
  101. {
  102.   int i;
  103.   if(_data!=NULL){
  104.     for(i=0;i<_nRows;i++){
  105.       delete []_data[i];
  106.     }
  107.     delete []_data;
  108.   }
  109. }
  110.  
  111. //////////////////////////
  112. //
  113. //////////////////////////
  114. template<class Type>
  115. void Matrix<Type>::resize(unsigned int nRows, unsigned int nCols)
  116. {
  117.   int i;
  118.   if(_data!=NULL){
  119.     for(i=0;i<nRows;i++){
  120.       delete []_data[i];
  121.     }
  122.     delete []_data;
  123.   }
  124.  
  125.   _nRows=nRows;
  126.   _nCols=nCols;
  127.  
  128.   _data=NULL;
  129.     _data=new Type *[_nRows];
  130.     for(i=0;i<_nRows;i++)
  131.       _data[i]=new Type[_nCols];
  132.   }
  133. }
  134.  
  135. //////////////////////////
  136. //
  137. //////////////////////////
  138. template<class Type>
  139. Type & Matrix<Type>::get(unsigned int r, unsigned int c)
  140. {
  141.   assert(0<r<_nRows);
  142.   assert(0<r<_nCols);
  143.   return _data[r][c];
  144. }
  145.  
  146. //////////////////////////
  147. //
  148. //////////////////////////
  149. template<class Type>
  150. void Matrix<Type>::setIdentity()
  151. {
  152.   int i,j;
  153.   for(i=0;i<_nRows;i++){
  154.     for(j=0;j<_nCols;j++){
  155.       if(i==j)
  156.     _data[i][j]=1;
  157.       else
  158.     _data[i][j]=0;
  159.     }
  160.   }
  161. }
  162.  
  163. //////////////////////////
  164. //
  165. //////////////////////////
  166. template<class Type>
  167. Matrix<Type> & Matrix<Type>::operator=(const Matrix &M)
  168. {
  169.   int i,j;
  170.   if(_nRows==M._nRows && _nCols==M._nCols)
  171.   for(i=0;i<_nRows;i++)
  172.     for(j=0;j<_nCols;j++)
  173.       _data[i][j]=M._data[i][j];
  174.   return *this;
  175. }
  176.  
  177. //////////////////////////
  178. //
  179. //////////////////////////
  180. template<class Type>
  181. Matrix<Type> Matrix<Type>::operator+(const Matrix &M)
  182. {
  183.   assert(_nRows==M._nRows && _nCols==M._nCols);
  184.   Matrix<Type> aux(M._nRows, M._nCols);
  185.   int i,j;
  186.   for(i=0;i<_nRows;i++){
  187.     for(j=0;j<_nCols;j++){
  188.       aux._data[i][j]=_data[i][j]+M._data[i][j];
  189.     }
  190.   }
  191.   return aux;//no se si esto se puede hacer
  192. }
  193. };
  194.  
  195.  
  196. #endif
  197.  


Este es el main.cpp para probrarlo:

Código: C++
  1. #include <matrix.h>
  2. #include <iostream>
  3. using namespace std;
  4. using namespace storage;
  5.  
  6. //template Function that prints a matrix
  7.  
  8. template<class Type>
  9. void print(Matrix<Type> &M)
  10. {
  11.   for(unsigned int i=0;i<M.getNRows();i++){
  12.   for(unsigned int j=0;j<M.getNCols();j++)
  13.   cout<<M.get(i,j)<< " ";
  14.   cout<<endl;
  15.   }
  16.  cout<<endl;
  17. }
  18.  
  19. int main(int argc,char **argv)
  20. {
  21.   Matrix<float> M;
  22.   M.resize(10,10);
  23.   M.setIdentity();
  24.   print(M);
  25.   Matrix<float> M2(M);
  26.   print(M2);
  27.   Matrix<float> M3(10,10);
  28.   M3=M+M2;
  29.   print(M3);
  30.   //check the result
  31.   for(unsigned int i=0;i<M3.getNRows();i++){
  32.   for(unsigned int j=0;j<M.getNCols();j++)
  33.   if (i!=j)
  34.   assert(M3.get(i,j)==0);
  35.   else assert(M3.get(i,j)==2);
  36.   }
  37.   cout<<"Perfect"<<endl;
  38. }

ElJava

  • Nuevo Miembro
  • *
  • Mensajes: 2
    • Ver Perfil
Re:Clase matriz con plantillas
« Respuesta #1 en: Lunes 29 de Agosto de 2011, 19:44 »
0
No he revisado tu código, pero un fallo muy usual (también lo he cometido yo varias veces) es declarar un template en un archivo .h, sólo puede declararse todo en un mismo .cpp
Un saludo!