Programación General > C/C++

 Clase matriz con plantillas

(1/1)

antlcn:
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++ ---#ifndef _Matrix_H_#define _Matrix_H_#include <cassert>#include <cstdlib> using namespace std;namespace storage{    template<class Type>        class Matrix        {            public:                /* constructor vacio                 */                inline Matrix();                /* constructor copia                 */                inline Matrix(const Matrix &M);                /* constructor parametrizado                 */                inline Matrix(unsigned int nRows, unsigned int nCols);                /* destructor                 */                inline ~Matrix();                /* redimensionar la matriz                 */                             inline void resize(unsigned int nRows, unsigned int nCols);                /* indica el numero de filas                */                unsigned int getNRows() { return _nRows;}                /* indica el numero de columnas                 */                unsigned int getNCols() { return _nCols;}                /* retorna el valor indicado por las variables                 */                inline Type & get(unsigned int r,unsigned int c);                /* hace que la matriz sea la indentidad                 */                inline void setIdentity();                /* operador de asignacion                 */                inline Matrix & operator=(const Matrix &M);                /* realiza la suma de dos matrices                 */                inline Matrix operator+(const Matrix &M);                                       private:            Type **_data;            unsigned int _nRows;            unsigned int _nCols;        };         //////////////////////////////////////////////////////template<class Type>Matrix<Type>::Matrix(){  _data=NULL;  _nRows=0;  _nCols=0;} //////////////////////////////////////////////////////template<class Type>Matrix<Type>::Matrix(const Matrix &M){  int i=0,j=0;  _data=NULL;  resize(M._nRows, M._nCols);    for(i=0;i<_nRows;i++)  {    for(j=0;j<_nCols;j++)    {      _data[i][j]=M._data[i][j];    }  }} //////////////////////////////////////////////////////template<class Type>Matrix<Type>::Matrix(unsigned int nRows, unsigned int nCols){  _data=NULL;  resize(nRows, nCols);}  //////////////////////////////////////////////////////template<class Type>Matrix<Type>::~Matrix(){  int i;  if(_data!=NULL){    for(i=0;i<_nRows;i++){      delete []_data[i];    }    delete []_data;  }} //////////////////////////////////////////////////////template<class Type>void Matrix<Type>::resize(unsigned int nRows, unsigned int nCols){  int i;  if(_data!=NULL){    for(i=0;i<nRows;i++){      delete []_data[i];    }    delete []_data;  }    _nRows=nRows;  _nCols=nCols;   _data=NULL;    _data=new Type *[_nRows];    for(i=0;i<_nRows;i++)      _data[i]=new Type[_nCols];  }} //////////////////////////////////////////////////////template<class Type>Type & Matrix<Type>::get(unsigned int r, unsigned int c){  assert(0<r<_nRows);  assert(0<r<_nCols);  return _data[r][c];} //////////////////////////////////////////////////////template<class Type>void Matrix<Type>::setIdentity(){  int i,j;  for(i=0;i<_nRows;i++){    for(j=0;j<_nCols;j++){      if(i==j)    _data[i][j]=1;      else    _data[i][j]=0;    }  }} //////////////////////////////////////////////////////template<class Type>Matrix<Type> & Matrix<Type>::operator=(const Matrix &M){  int i,j;  if(_nRows==M._nRows && _nCols==M._nCols)  for(i=0;i<_nRows;i++)    for(j=0;j<_nCols;j++)      _data[i][j]=M._data[i][j];  return *this;} //////////////////////////////////////////////////////template<class Type>Matrix<Type> Matrix<Type>::operator+(const Matrix &M){  assert(_nRows==M._nRows && _nCols==M._nCols);  Matrix<Type> aux(M._nRows, M._nCols);  int i,j;  for(i=0;i<_nRows;i++){    for(j=0;j<_nCols;j++){      aux._data[i][j]=_data[i][j]+M._data[i][j];    }  }  return aux;//no se si esto se puede hacer}};  #endif 

Este es el main.cpp para probrarlo:


--- Código: C++ ---#include <matrix.h>#include <iostream>using namespace std;using namespace storage; //template Function that prints a matrix template<class Type>void print(Matrix<Type> &M){  for(unsigned int i=0;i<M.getNRows();i++){  for(unsigned int j=0;j<M.getNCols();j++)  cout<<M.get(i,j)<< " ";  cout<<endl;  } cout<<endl;} int main(int argc,char **argv){  Matrix<float> M;  M.resize(10,10);  M.setIdentity();  print(M);  Matrix<float> M2(M);  print(M2);  Matrix<float> M3(10,10);  M3=M+M2;  print(M3);  //check the result  for(unsigned int i=0;i<M3.getNRows();i++){  for(unsigned int j=0;j<M.getNCols();j++)  if (i!=j)  assert(M3.get(i,j)==0);  else assert(M3.get(i,j)==2);  }  cout<<"Perfect"<<endl;}

ElJava:
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!

Navegación

[0] Índice de Mensajes

Ir a la versión completa