class -> template

Ales Bardorfer alesb at robo.fe.uni-lj.si
Thu Apr 26 02:16:42 CEST 2001


Napisal sem en class, ki sem ga zelel spremeniti v template class.

class doubleMatrix {
...
  doubleMatrix(int nr, int nc) {...}
  ~doubleMatrix() {...}
  
  double& operator () (const int r, const int c);
};

double & 
doubleMatrix::operator () (const int r, const int c) {...}



To sem spremenil v:

template <class T>
class Matrix 
{...
  Matrix(int nr, int nc) {...}
  ~Matrix() {...}
  
  T & operator () (const int r, const int c);
};

template <class T>
T & 
Matrix<T>::operator () (const int r, const int c) {...}



Obe varianti sta na dveh datotekah (matrix.h in matrix.cpp),
mat_test.cpp pa je testni program.
Prvi primer deluje brez problemov. Drugi (template) pa javi napako ob
linkanju:

g++ -c  -I. mat_test.cpp -o mat_test.o
g++ -c  -I. matrix.cpp -o matrix.o
g++ mat_test.o matrix.o  -o mat_test
mat_test.o: In function `main':
mat_test.o(.text+0x26): undefined reference to
`Matrix<double>::operator()(int, int)'
mat_test.o(.text+0x48): undefined reference to
`Matrix<double>::operator()(int, int)'
collect2: ld returned 1 exit status
make: *** [mat_test] Error 1



Kje sem ga polomil? 

lp,
Ales




More information about the lugos-prog mailing list