Template de class et signature de fonction
Bonjour à tous,
Aujourd'hui j'ai un soucis avec l'utilisation des templates, voici d'abord mon code :
Matrix.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #include <vector>
#include <iostream>
#ifndef MATRIX
#define MATRIX
template<typename T>
class Matrix
{
private:
std::vector<std::vector<T> > data;
public:
Matrix();
Matrix(int swidth, int sheight);
Matrix(int size);
int getWidth() { return data.size(); }
int getHeight() { return data.size() == 0 ? 0 : data[0].size(); }
void print();
~Matrix();
};
#endif |
Matrix.cpp (simplifié)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include "Matrix.h"
/*********************************
C O N S T R U C T O R S
**********************************/
template<class T>
Matrix<T>::Matrix(){
data.empty();
std::clog << "Construction matrice" << std::endl;
}
template<class T>
Matrix<T>::Matrix(int swidth, int sheight) {
data.resize(0);
if(swidth > 0 && sheight > 0) {
data.resize(swidth);
for(unsigned int i = 0; i < data.size(); i++) {
data[i].resize(sheight);
}
std::clog << "Construction matrice" << std::endl;
}
}
template<class T>
Matrix<T>::Matrix(int size) {
if(size > 0) {
data.resize(size);
for(unsigned int i = 0; i < data.size(); i++) {
data[i].resize(size);
}
} else {
data.resize(0);
}
std::clog << "Construction matrice" << std::endl;
}
/*********************************
O T H E R
**********************************/
template<class T>
void Matrix<T>::print() {
int height;
height = data.size() == 0 ? 0 : data[0].size();
std::cout << "Width = " << height << " height = "<< data.size() << std::endl;
for(unsigned int i = 0; i < data.size(); i++) {
for (unsigned int j = 0; j < data[i].size(); j++) {
std::cout << data[i][j] << " ";
}
std::cout << std::endl;
}
}
/*********************************
D E S T R U C T O R
**********************************/
template<class T>
Matrix<T>::~Matrix() {
data.resize(0);
std::clog << "Destruction matrice" << std::endl;
} |
Et enfin le main.cpp
Code:
1 2 3 4 5 6 7 8
| #include <iostream>
#include "Matrix.h"
int main() {
Matrix<int> m1(5);
return 0;
} |
Voici ce me renvoie le compilateur (g++) :
Code:
1 2 3 4 5
| main.o: In function `main':
/home/isen/Cours/C/perso/matrix/main.cpp:5: undefined reference to `Matrix<int>::Matrix(int)'
/home/isen/Cours/C/perso/matrix/main.cpp:5: undefined reference to `Matrix<int>::~Matrix()'
collect2: ld a retourné 1 code d'état d'exécution
make: *** [matrix] Erreur 1 |
Il ne semble pas reconnaître les fonctions, cela ne marche pas non plus avec m1.print();. Je dois mal utiliser les templates mais même après avoir cherché je ne trouve pas de solutions.
PS : Si je n'écrit pas template<class T> void Matrix<T>::print()(...) dans le Matrix.cpp, le compilateur me cri dessus alors que cette fonction se moque royalement du type T. Je ne vois pas pourquoi je dois écrire cela.