 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
D!m Guest
|
Posted: Sun Jun 26, 2005 10:11 pm Post subject: heritage et template |
|
|
Bonjour,
je ne comprends pas pourquoi ce code ne compile pas avec g++, pourtant B
herite de A.
$ g++ -c C.cpp
In file included from C.cpp:1:
B.hpp:25: error: no `void B<T>::insert(const T&)' member function
declared in class `B<T>'
B.hpp:25: error: template definition of non-template `void
B<T>::insert(const T&)'
Fichier C.cpp
-----------------
#include "B.hpp"
int main(int argc, char *argv[])
{
B<int> toto;
toto.insert(5);
return 0;
}
-----------------
Fichier B.hpp
-----------------
#ifndef B_hpp
#define B_hpp
#include "A.hpp"
template <class T> class B : public A<T>
{
public:
B(unsigned int n=100);
~B();
private:
B(const B&);
B& operator=(const B&);
};
template <class T> B<T>::B(unsigned int n) : A<T>(n)
{
}
template <class T> B<T>::~B()
{
}
template <class T> void B<T>::insert(const T& anElt)
{
}
#endif
------------------
Fichier A.hpp
------------------
#ifndef A_hpp
#define A_hpp
template <class T> class A
{
public:
A(unsigned int n=100);
~A();
void insert(const T& anElt);
private:
A(const A&);
A& operator=(const A&);
};
template <class T> A<T>::A(unsigned int n)
{
}
template <class T> A<T>::~A()
{
}
template <class T> void A<T>::insert(const T& anElt)
{
}
#endif
------------------
--
|
|
| Back to top |
|
 |
Fabien LE LEZ Guest
|
Posted: Sun Jun 26, 2005 10:57 pm Post subject: Re: heritage et template |
|
|
On Mon, 27 Jun 2005 00:11:14 +0200, "D!m" <dim (AT) toto (DOT) fr>:
| Quote: | template <class T> void B<T>::insert(const T& anElt)
|
Le fait que B dérive de A signifie juste que B::insert est
implicitement défini comme étant identique à A::insert.
Si tu veux la définir explicitement, tu dois la déclarer
explicitement :
template <class T> class B: ...
{
...
void insert(const T& anElt);
...
};
Note : ce n'est pas particulier aux templates.
|
|
| Back to top |
|
 |
adebaene@club-internet.fr Guest
|
Posted: Mon Jun 27, 2005 2:50 pm Post subject: Re: heritage et template |
|
|
D!m a écrit :
| Quote: | Bonjour,
je ne comprends pas pourquoi ce code ne compile pas avec g++, pourtant B
herite de A.
$ g++ -c C.cpp
In file included from C.cpp:1:
B.hpp:25: error: no `void B<T>::insert(const T&)' member function
declared in class `B<T>'
B.hpp:25: error: template definition of non-template `void
B<T>::insert(const T&)'
snip
Fichier B.hpp
-----------------
#ifndef B_hpp
#define B_hpp
#include "A.hpp"
template <class T> class B : public A<T
{
public:
B(unsigned int n=100);
~B();
private:
B(const B&);
B& operator=(const B&);
};
template
{
}
template <class T> B<T>::~B()
{
}
template <class T> void B<T>::insert(const T& anElt)
{
}
|
Pourquoi est-ce que tu définis B::insert ici? Tu ne l'a pas déclaré
dans la classe B, et à priori ce que tu veux c'est juste hériter de
la version déclarée et définie dans A, non?
Arnaud
|
|
| 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
|
|