 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Vincent Richard Guest
|
Posted: Mon Jan 03, 2005 7:57 pm Post subject: [g++] Spécialisation de template et erreurau link |
|
|
Bonsoir,
J'ai un petit problème pour compiler et linker le code suivant avec
la version 3.2 de GNU g++. Cela fonctionne sans problème avec les
versions suivantes (3.3 et 3.4) :
a.hpp (déclarations)
=====
template <typename TYPE>
struct A
{
void f() { /* ... */ }
TYPE v;
};
template <> void A <int>::f();
a1.cpp (définition de f() pour le type 'int')
======
#include "a.hpp"
template <> void A <int>::f()
{
// ...
}
a2.cpp (main)
======
#include "a.hpp"
int main()
{
A <int> a;
a.f();
}
Je compile avec la commande suivante :
$ g++ -o prog a1.cpp a2.cpp
L'erreur obtenue est la suivante (au link) :
/tmp/ccFY1RVe.o(.text+0x18): In function `main':
: undefined reference to `A<int>::f()'
collect2: ld returned 1 exit status
Est-ce un bug ? (apparement oui, car ça fonctionne dans les versions
suivantes) Y'a-t-il une astuce pour contourner ça, autre que de
définir la fonction dans le .hpp (en inline) ?
Merci d'avance pour vos réponses.
Vincent
|
|
| Back to top |
|
 |
Pierre Lairez Guest
|
Posted: Tue Jan 04, 2005 5:48 pm Post subject: Re: [g++] Spécialisation de template et erreur au link |
|
|
La syntaxe n'est pas
template <> void A <int>::f();
mais
template void A <int>::f();
Cependant, ton problème m'étonne, puisque A <int>::f() est déjà déclaré
explicitement, il ne devrait pas y avoir besoin du prototype...
Le Mon, 3 Jan 2005 20:57:29 +0100, Vincent Richard
<chere-loque (AT) plop (DOT) wanadoo.fr.invalid> a écrit:
| Quote: | Bonsoir,
J'ai un petit problème pour compiler et linker le code suivant avec
la version 3.2 de GNU g++. Cela fonctionne sans problème avec les
versions suivantes (3.3 et 3.4) :
a.hpp (déclarations)
=====
template <typename TYPE
struct A
{
void f() { /* ... */ }
TYPE v;
};
template <> void A <int>::f();
a1.cpp (définition de f() pour le type 'int')
======
#include "a.hpp"
template <> void A <int>::f()
{
// ...
}
a2.cpp (main)
======
#include "a.hpp"
int main()
{
A <int> a;
a.f();
}
Je compile avec la commande suivante :
$ g++ -o prog a1.cpp a2.cpp
L'erreur obtenue est la suivante (au link) :
/tmp/ccFY1RVe.o(.text+0x18): In function `main':
: undefined reference to `A<int>::f()'
collect2: ld returned 1 exit status
Est-ce un bug ? (apparement oui, car ça fonctionne dans les versions
suivantes) Y'a-t-il une astuce pour contourner ça, autre que de
définir la fonction dans le .hpp (en inline) ?
Merci d'avance pour vos réponses.
Vincent
|
|
|
| Back to top |
|
 |
|
|
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
|
|