Structure des fichiers avec des templates
Bonjour,
J'essaie de structurer un maximum un programme et je me retrouve avec un problème lorsque j'utilise les templates:
Fichier A.h:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #ifndef A_H
# define A_H
template <class T>
class A
{
public:
A();
virtual ~A();
private:
T value;
};
#endif /* A_H */ |
Fichier A.cpp:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <iostream>
#include "A.h"
template <class T>
A<T>::A() : value(0)
{
std::cout << "Constructeur de A" << std::endl;
}
template <class T>
A<T>::~A()
{
std::cout << "Destructeur de A" << std::endl;
} |
Fichier B.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| #ifndef B_H
# define B_H
# include "A.h"
class B : public A<int>
{
public:
B();
~B();
};
#endif /* B_H */ |
Fichier B.cpp:
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| #include <iostream>
#include "B.h"
B::B()
{
std::cout << "Constructeur de B" << std::endl;
}
B::~B()
{
std::cout << "Destructeur de B" << std::endl;
} |
Fichier main.cpp:
Code:
1 2 3 4 5 6 7 8 9
| #include "A.h"
#include "B.h"
int main()
{
A<int> *a = new B;
delete a;
return 0;
} |
Lorsque je compile, j'ai l'erreur suivante :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| /tmp/ccM9wBFi.o: In function `B::~B()':
B.cpp:(.text+0x1be): undefined reference to `A<int>::~A()'
B.cpp:(.text+0x1d7): undefined reference to `A<int>::~A()'
/tmp/ccM9wBFi.o: In function `B::~B()':
B.cpp:(.text+0x246): undefined reference to `A<int>::~A()'
B.cpp:(.text+0x25f): undefined reference to `A<int>::~A()'
/tmp/ccM9wBFi.o: In function `B::~B()':
B.cpp:(.text+0x2ce): undefined reference to `A<int>::~A()'
/tmp/ccM9wBFi.o:B.cpp:(.text+0x2e7): more undefined references to `A<int>::~A()' follow
/tmp/ccM9wBFi.o: In function `B::B()':
B.cpp:(.text+0x322): undefined reference to `A<int>::A()'
B.cpp:(.text+0x363): undefined reference to `A<int>::~A()'
/tmp/ccM9wBFi.o: In function `B::B()':
B.cpp:(.text+0x38a): undefined reference to `A<int>::A()'
B.cpp:(.text+0x3cb): undefined reference to `A<int>::~A()'
collect2: ld a retourné 1 code d'état d'exécution |
Le compilateur ne trouve pas la définition du destructeur de A. Par contre, lorsque je fais la même chose pour une version non templètisée, alors je n'ai aucun soucis.
Quelqu'un pourrait m'expliquer d'où provient le problème?
Merci d'avance