Bonjour,
je cherche à déclarer une relation d'amitié entre une classe générique (template) et une surcharge de l'opérateur d'extraction de flux (operator>>) mais j'obtiens une erreur.
J'ai l'habitude des relations d'amitié entre une classe générique et une surcharge de l'opérateur d'insertion de flux (operator<<) :
Ce code fonctionne très bien sur ma machine et la fonction principale
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 #ifndef FOO_HPP #define FOO_HPP #include <ostream> template<class T> class Foo; template<class T> std::ostream & operator<<(std::ostream &, Foo<T> const &); template<class T> class Foo { friend std::ostream & operator<< <>(std::ostream &, Foo const &); public: Foo(T const & value) :value_(value) {} private: T value_; }; template<class T> std::ostream & operator<<(std::ostream & os, Foo<T> const & bar) { os << bar.value_; return os; } #endif
affiche bien 3.14.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 #include "foo.hpp" #include <iostream> int main() { Foo<double> foo(3.14); std::cout << foo << std::endl; return 0; }
En revanche, si je tente de faire la même chose avec l'opérateur d'extraction, comme dans
j'obtiens l'erreur de compilation suivante avec g++ :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 #ifndef FOO_HPP #define FOO_HPP #include <istream> #include <ostream> template<class T> class Foo; template<class T> std::istream & operator>>(std::istream &, Foo<T> &); template<class T> std::ostream & operator<<(std::ostream &, Foo<T> const &); template<class T> class Foo { friend std::istream & operator<< <>(std::istream &, Foo &); friend std::ostream & operator<< <>(std::ostream &, Foo const &); public: Foo(T const & value) :value_(value) {} private: T value_; }; template<class T> std::ostream & operator<<(std::ostream & os, Foo<T> const & bar) { os << bar.value_; return os; } template<class T> std::istream & operator>>(std::istream & is, Foo<T> & bar) { is >> bar.value_; return is; } #endif
Quelqu'un peut-il m'expliquer pourquoi?In file included from main.cpp:2:0:
foo.hpp: In instantiation of ‘Foo<double>’:
main.cpp:8:18: instantiated from here
foo.hpp:19:25: erreur: template-id ‘operator<< <>’ for ‘std::istream& operator<<(std::istream&, Foo<double>&)’ does not match any template declaration
D'avance, merci beaucoup!
Partager