C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

template et typname

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French)
View previous topic :: View next topic  
Author Message
Djédjé
Guest





PostPosted: Fri Jun 27, 2003 9:08 pm    Post subject: template et typname Reply with quote



J'ai un petit souci avec un template qui pourtant d'après les exemples
et les tutoriels que j'ai consultés devrait se linker correctemment.
Voici le problème réduit à un tout petit exemple :

le programme principal :

#include "../include/CMatrice.h"

int main(int argc, char** argv)
{
CMatrice<bool> matrice(10);

matrice.getMatrice(1,1);

return 0;
}

Le fichier en-tête :

#ifndef _cmatrice_h
#define _cmatrice_h

template <typename T>
class CMatrice
{
public:
CMatrice(long taille);
~CMatrice();
T getMatrice(long pos1, long pos2);
void setMatrice(long pos1, long pos2, T valeur);

long getN();

private:
T** matrice; // matrice carree N*N de type patron "T"
long N; // taille du cote de la matrice
};

#endif

J'ai essayé avec le mot-clef class à la place de typename, mais rien y
fait, j'ai toujours l'erreur suivante :

$ g++ -Wall -g "test.cpp" -o "test"
/tmp/ccjrBb40.o(.text+0x1b): In function `main':
/home/prog-x/Projects/essai/src/test.cpp:5: undefined reference to
`CMatrice<bool>::CMatrice[in-charge](long)'
/tmp/ccjrBb40.o(.text+0x2e):/home/prog-x/Projects/essai/src/test.cpp:7:
undefined reference to `CMatrice<bool>::getMatrice(long, long)'
/tmp/ccjrBb40.o(.text+0x3d):/home/prog-x/Projects/essai/src/test.cpp:9:
undefined reference to `CMatrice<bool>::~CMatrice [in-charge]()'
/tmp/ccjrBb40.o(.text+0x54):/home/prog-x/Projects/essai/src/test.cpp:9:
undefined reference to `CMatrice<bool>::~CMatrice [in-charge]()'
collect2: ld returned 1 exit status

Quelqu'un à une idée de la source et/ou du genre de problème que je
rencontre?
Back to top
Christophe Lephay
Guest





PostPosted: Fri Jun 27, 2003 10:17 pm    Post subject: Re: template et typname Reply with quote



"Djédjé" <oxy (AT) isuisse (DOT) com> a écrit dans le message de
news:392de28a.0306271308.5e459731 (AT) posting (DOT) google.com...
Quote:
J'ai un petit souci avec un template qui pourtant d'après les exemples
et les tutoriels que j'ai consultés devrait se linker correctemment.
Voici le problème réduit à un tout petit exemple :

le programme principal :

#include "../include/CMatrice.h"

int main(int argc, char** argv)
{
CMatrice<bool> matrice(10);

matrice.getMatrice(1,1);

return 0;
}

Le fichier en-tête :

#ifndef _cmatrice_h
#define _cmatrice_h

template <typename T
class CMatrice
{
public:
CMatrice(long taille);
~CMatrice();
T getMatrice(long pos1, long pos2);
void setMatrice(long pos1, long pos2, T valeur);

long getN();

private:
T** matrice; // matrice carree N*N de type patron "T"
long N; // taille du cote de la matrice
};

#endif

J'ai essayé avec le mot-clef class à la place de typename, mais rien y
fait, j'ai toujours l'erreur suivante :

$ g++ -Wall -g "test.cpp" -o "test"
/tmp/ccjrBb40.o(.text+0x1b): In function `main':
/home/prog-x/Projects/essai/src/test.cpp:5: undefined reference to
`CMatrice /tmp/ccjrBb40.o(.text+0x2e):/home/prog-x/Projects/essai/src/test.cpp:7:
undefined reference to `CMatrice<bool>::getMatrice(long, long)'
/tmp/ccjrBb40.o(.text+0x3d):/home/prog-x/Projects/essai/src/test.cpp:9:
undefined reference to `CMatrice<bool>::~CMatrice [in-charge]()'
/tmp/ccjrBb40.o(.text+0x54):/home/prog-x/Projects/essai/src/test.cpp:9:
undefined reference to `CMatrice<bool>::~CMatrice [in-charge]()'
collect2: ld returned 1 exit status

Quelqu'un à une idée de la source et/ou du genre de problème que je
rencontre?

Il faut inclure le fichier contenant les définitions de tes templates
(cmatrice.c ou .cc ou .cpp)

Chris



Back to top
Djédjé
Guest





PostPosted: Sat Jun 28, 2003 11:06 am    Post subject: Re: template et typname Reply with quote



"Christophe Lephay" <christophe-lephay (AT) wanadoo (DOT) fr> wrote

Quote:
"Djédjé" <oxy (AT) isuisse (DOT) com> a écrit dans le message de
news:392de28a.0306271308.5e459731 (AT) posting (DOT) google.com...
J'ai un petit souci avec un template qui pourtant d'après les exemples
et les tutoriels que j'ai consultés devrait se linker correctemment.
Voici le problème réduit à un tout petit exemple :

le programme principal :

#include "../include/CMatrice.h"

int main(int argc, char** argv)
{
CMatrice<bool> matrice(10);

matrice.getMatrice(1,1);

return 0;
}

Le fichier en-tête :

#ifndef _cmatrice_h
#define _cmatrice_h

template <typename T
class CMatrice
{
public:
CMatrice(long taille);
~CMatrice();
T getMatrice(long pos1, long pos2);
void setMatrice(long pos1, long pos2, T valeur);

long getN();

private:
T** matrice; // matrice carree N*N de type patron "T"
long N; // taille du cote de la matrice
};

#endif

J'ai essayé avec le mot-clef class à la place de typename, mais rien y
fait, j'ai toujours l'erreur suivante :

$ g++ -Wall -g "test.cpp" -o "test"
/tmp/ccjrBb40.o(.text+0x1b): In function `main':
/home/prog-x/Projects/essai/src/test.cpp:5: undefined reference to
`CMatrice /tmp/ccjrBb40.o(.text+0x2e):/home/prog-x/Projects/essai/src/test.cpp:7:
undefined reference to `CMatrice<bool>::getMatrice(long, long)'
/tmp/ccjrBb40.o(.text+0x3d):/home/prog-x/Projects/essai/src/test.cpp:9:
undefined reference to `CMatrice<bool>::~CMatrice [in-charge]()'
/tmp/ccjrBb40.o(.text+0x54):/home/prog-x/Projects/essai/src/test.cpp:9:
undefined reference to `CMatrice<bool>::~CMatrice [in-charge]()'
collect2: ld returned 1 exit status

Quelqu'un à une idée de la source et/ou du genre de problème que je
rencontre?

Il faut inclure le fichier contenant les définitions de tes templates
(cmatrice.c ou .cc ou .cpp)

Chris


Oui je l'avais mis, je ne l'ai simplement pas rajouté ici pour ne pas
surcharger trop mon post. Le CMatrice.cpp est le suivant :

#include "../include/CMatrice.h"

template <typename T>
CMatrice<T>::CMatrice(long taille)
{
long i = 0;
N = taille;

matrice = new T*[N];
for (i = 0; i < N; i++) matrice[i] = new T[N];
}
template CMatrice<T>::~CMatrice()
{
for(long i=0; i<N; i++ )
delete []matrice[i];
delete []matrice;
}
template T CMatrice<T>::getMatrice(long pos1, long pos2)
{
try{return matrice[pos1][pos2];}
catch(...){printf("indice de matrice (%ld ou %ld)
invaliden",pos1,pos2);}
}

template <typename T>
void CMatrice<T>::setMatrice(long pos1, long pos2, T valeur)
{
try{matrice[pos1][pos2]= valeur;}
catch(...){printf("indice de matrice (%ld ou %ld)
invaliden",pos1,pos2);}
}


template <typename T>
long CMatrice<T>::getN(){return N;}

Là tout est défini, ça passe à la compil, mais à l'édition de lien il
n'est pas content du tout...

Back to top
Vincent Lascaux
Guest





PostPosted: Sat Jun 28, 2003 11:20 am    Post subject: Re: template et typname Reply with quote

Quote:
Oui je l'avais mis, je ne l'ai simplement pas rajouté ici pour ne pas
surcharger trop mon post. Le CMatrice.cpp est le suivant :

Comme pour les fonctions inline, il ne doit pas y avoir une unité de
compilation séparée pour les templates. Il ne faut pas que tu compiles ton
CMatrice.cpp, et il faut que tu ajoutes à la fin de ton header un
#include "CMatric.cpp"

La convention est souvent de renomer CMatrice.cpp en CMatrice.inl



Back to top
Michaël Monerau
Guest





PostPosted: Sat Jun 28, 2003 5:07 pm    Post subject: Re: template et typname Reply with quote

Djédjé wrote:
Quote:
void setMatrice(long pos1, long pos2, T valeur);

Au passage, préfère :

void setMatrice(long pos1, long pos2, const T& valeur);

Ca permet de ne pas copier l'objet valeur. Si T est un type pour lequel la
copie coûte cher en temps ou en resources, c'est préférable Smile
--
<-= Michaël "Cortex" Monerau =->



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.