Surcharge de << avec une class Template ?
Bonjour,
j'ai un petit probleme de surcharge de l'operateur <<
voila donc j'aimerais pouvoir afficher une matrix dans la console (max 10x10)
le main.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
#include "MptMatrix.hpp"
using namespace std;
main () {
...
MptMatrix<uint32> meanMx(height+1,width+1);
meanMx.loadPad2DImage(bw_img);
cout << meanMx << endl;
...
}
.... |
et voici le code de MptMatrix.hpp
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 69 70 71 72 73 74 75 76 77 78 79
|
#ifndef MptMATRIX_H
#define MptMATRIX_H
#include <c4v.h>
#include <iostream>
template <class T>
class MptMatrix {
public:
// Default Constructor
MptMatrix(void);
// Constructor by Copy
MptMatrix(const MptMatrix& initMx);
// Constructor
MptMatrix(int r, int c, bool scanHori=true);
// Destructor
~MptMatrix();
// Get function
inline int getRows();
inline int getCols();
inline bool scanHorizontal();
inline int getScan1Size();
inline int getScan2Size();
void create(int r, int c, bool scanHori=true);
void loadImage(IplImage * img);
void loadPad2DImage(IplImage* img);
//Surcharge function
friend std::ostream& operator<<(std::ostream&, MptMatrix<T>&);
private:
T *_pdata;
int _rows, _cols;
bool _scanHori;
};
template <class T>
inline int MptMatrix<T>::getRows() { return _rows; }
template <class T>
inline int MptMatrix<T>::getCols() { return _cols; }
template <class T>
inline bool MptMatrix<T>::scanHorizontal() { return _scanHori; }
template <class T>
inline int MptMatrix<T>::getScan1Size() { return (_scanHori)? _cols : _rows;}
template <class T>
inline int MptMatrix<T>::getScan2Size() { return (_scanHori)? _rows : _cols;}
template <class T>
std::ostream& operator<<(std::ostream& os, MptMatrix<T>& mX)
{
int tmp;
os << "Matrix (w x h) :" << mX._rows<< " x " << mX._cols;
if(!mX._scanHori) os << "VERTICAL SCAN" << std::endl;
for(int i(0); i<10;i++) {
tmp = i*mX.getScan1Size();
for(int j(0);j<10;j++) {
os << mX._pdata[tmp+j] << " ";
}
os << std::endl;
}
os << std::endl;
return os;
}
#include "MptMatrix.cpp"
#endif |
Et l'erreur de compilation c'est :
Code:
1 2 3 4 5 6 7 8
|
neub@hpdv1000:~/projetDS/openCV$ make
g++ -Iinclude `pkg-config opencv --cflags` -g -D_DEBUG -c src/main.cpp -o obj/main.o
src/MptMatrix.hpp:34: attention : friend declaration «std::ostream& operator<<(std::ostream&, MptMatrix<T>&)» declares a non-template function
src/MptMatrix.hpp:34: attention : (si ce n'est pas ce que vous vouliez faire, soyez sûr que le patron de la fonction a déjà été déclaré et ajouter <> après le nom de la fonction ici) -Wno-non-template-friend désactive le présent avertissement
obj/main.o : Dans la fonction "imageProcess(char const*)":src/main.cpp:59: référence indéfinie vers « operator<<(std::basic_ostream<char, std::char_traits<char> >&, MptMatrix<unsigned int>&)»
collect2: ld a retourné 1 code d'état d'exécution
make: *** [trackpatch] Erreur 1 |
Alors voila j'ai 2 questions :
- Pourquoi je n'ai pas besoin du template. Je sais que ce n'est pas une classe appartenant a MptMatrix mais je doit quand meme lui dire de quel type c'est...
template <class T>
std::ostream& operator<<(std::ostream& os, MptMatrix<T>& mX)
Pourquoi ca marche mieux si il n'y a pas la template, pourtant MptMatrix<T>& mX a bien un T.
La je ne comprend pas bien et pas reussis a trouver qqc qui explique ca dans la faq!
- Pourquoi j'ai ma 2eme erreur
Dans la fonction "imageProcess(char const*)":src/main.cpp:59: référence indéfinie vers « operator<<(std::basic_ostream<char, std::char_traits<char> >&, MptMatrix<unsigned int>&)»
collect2: ld a retourné 1 code d'état d'exécution
Voila merci d'avance pour votre aide precieuse