Probleme d utilisation de template dans le cpp
le .h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| # ifndef _MATRIX
# define _MATRIX
# include "Matrix.h"
template<class U,int n>
Matrix<U,n>::Matrix(const int aNbElem[]){
if ( n!=0){
int Nb=0;
NbElem = new int[n];
for(int i=0;i<n;i++) { NbElem[i]=aNbElem[i]; Nb*=NbElem[i]; }
if (Nb!=0) pMatrix= new U[Nb];
}
}
#endif |
le . cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| # ifndef _MATRIX
# define _MATRIX
#include <math.h>
template< class U,int n=1>
class Matrix{
public:
Matrix(const int aNbElem[]);
public:
int *NbElem;
U *pMatrix;
}; |
le message d'erreur est
c:\programcpp\stcdo\stcdoproject\cdopricing\headerfiles\matrix.cpp(27) : error C2143: syntax error : missing ';' before '<'
c:\programcpp\stcdo\stcdoproject\cdopricing\headerfiles\matrix.cpp(27) : error C2501: 'Matrix' : missing storage-class or type specifiers
c:\programcpp\stcdo\stcdoproject\cdopricing\headerfiles\matrix.cpp(27) : error C2059: syntax error : ';'
c:\programcpp\stcdo\stcdoproject\cdopricing\headerfiles\matrix.cpp(27) : error C2143: syntax error : missing ';' before '<'
Error executing cl.exe.
Ou fais je une erreur?
Re: Probleme d utilisation de template dans le cpp
Tu fais plusieurs erreurs. Je suppose dans ton post déjà, ou je présume qu'il faut inverser .h et .cpp.
Ensuite,
Code:
1 2 3 4 5 6
| # ifndef _MATRIX
# define _MATRIX
# include "Matrix.h"
// ... |
donc le code de Matrix.h va être inclus, ce qui donne:
Code:
1 2 3 4 5 6 7 8 9
| # ifndef _MATRIX
# define _MATRIX
# ifndef _MATRIX
# define _MATRIX
#include <math.h>
//... |
donc, à cause du #define _MATRIX dans le .cpp, le code du .h est ignoré.
http://c.developpez.com/faq/cpp/?pag...ASS_header_cpp
Enfin, un template, sauf cas très cpécial, ne doit pas être implémenté à part dans un .cpp, à moins de tricher.
http://c.developpez.com/faq/cpp/?pag...VERS_templates