Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2372 connectés 

 


Dernière réponse
Sujet : [C++]: Problèmes de templates avec la STL
Krueger Ah, ben oui ça marche en effet. Mais j'avais toujours préféré surcharger les opérateurs binaires voulus commutatifs par des fonctions globales...
 
Merci en tous cas. C'est bête, mais je n'y avait plus pensé à force d'automatiser la définition de ce genre de méthodes. :o

Votre réponse
Nom d'utilisateur    Pour poster, vous devez être inscrit sur ce forum .... si ce n'est pas le cas, cliquez ici !
Le ton de votre message                        
                       
Votre réponse


[b][i][u][strike][spoiler][fixed][cpp][url][email][img][*]   
 
   [quote]
 

Options

 
Vous avez perdu votre mot de passe ?


Vue Rapide de la discussion
Krueger Ah, ben oui ça marche en effet. Mais j'avais toujours préféré surcharger les opérateurs binaires voulus commutatifs par des fonctions globales...
 
Merci en tous cas. C'est bête, mais je n'y avait plus pensé à force d'automatiser la définition de ce genre de méthodes. :o
LeGreg pourquoi tu ne fais pas ca?

Code :
  1. template <class T> class A
  2. {
  3. public:
  4. A();
  5. A(const A &);
  6. bool operator ==  (const A& a);
  7. A operator+  (const A & a);
  8. };
  9. template <class T> A<T>::A<T>()
  10. {}
  11. template <class T> A<T>::A<T>(const A<T> & a)
  12. {}
  13. template <class T> bool A<T>::operator== (const A<T> & a)
  14. { return false;  }
  15. template <class T> A<T> A<T>::operator+  (const A<T> & c)
  16. { return A<T>(); }


 
LEGREG

Krueger Au cas où voilà ce que le compilo me jette:
 
 
/usr/include/g++-3/stl_iterator.h: In instantiation of `iterator_traits<int>':
/usr/include/g++-3/stl_iterator.h:574:   instantiated from `reverse_iterator<int>'
test.cpp:10:   instantiated from `A<int>'
test.cpp:25:   instantiated from here
/usr/include/g++-3/stl_iterator.h:102: `int' is not a class, struct, or union type
/usr/include/g++-3/stl_iterator.h:103: `int' is not a class, struct, or union type
/usr/include/g++-3/stl_iterator.h:104: `int' is not a class, struct, or union type
/usr/include/g++-3/stl_iterator.h:105: `int' is not a class, struct, or union type
/usr/include/g++-3/stl_iterator.h:106: `int' is not a class, struct, or union type
/usr/include/g++-3/stl_iterator.h: In instantiation of `reverse_iterator<int>':
test.cpp:10:   instantiated from `A<int>'
test.cpp:25:   instantiated from here
/usr/include/g++-3/stl_iterator.h:574: no type named `iterator_category' in `struct iterator_traits<int>'
/usr/include/g++-3/stl_iterator.h:576: no type named `value_type' in `struct iterator_traits<int>'
/usr/include/g++-3/stl_iterator.h:578: no type named `difference_type' in `struct iterator_traits<int>'
/usr/include/g++-3/stl_iterator.h:580: no type named `pointer' in `struct iterator_traits<int>'
/usr/include/g++-3/stl_iterator.h:582: no type named `reference' in `struct iterator_traits<int>'
/usr/include/g++-3/stl_iterator.h:599: no type named `reference' in `struct iterator_traits<int>'
/usr/include/g++-3/stl_iterator.h:604: no type named `pointer' in `struct iterator_traits<int>'
/usr/include/g++-3/stl_iterator.h:626: no type named `difference_type' in `struct iterator_traits<int>'
/usr/include/g++-3/stl_iterator.h:629: no type named `difference_type' in `struct iterator_traits<int>'
/usr/include/g++-3/stl_iterator.h:633: no type named `difference_type' in `struct iterator_traits<int>'
/usr/include/g++-3/stl_iterator.h:636: no type named `difference_type' in `struct iterator_traits<int>'
/usr/include/g++-3/stl_iterator.h:640: no type named `reference' in `struct iterator_traits<int>'
 
 
 
Je ne comprends pas pourquoi j'aides erreurs de la STL. Rien de suffisamment clair sur ce que j'ai codé. :(
 
Et si je remplace A<int> par A<string> ça donne:
 
 
/usr/include/g++-3/stl_iterator.h: In instantiation of `iterator_traits<string>':
/usr/include/g++-3/stl_iterator.h:574:   instantiated from `reverse_iterator<string>'
test.cpp:11:   instantiated from `A<string>'
test.cpp:26:   instantiated from here
/usr/include/g++-3/stl_iterator.h:102: no type named `iterator_category' in `class string'
/usr/include/g++-3/stl_iterator.h: In instantiation of `reverse_iterator<string>':
test.cpp:11:   instantiated from `A<string>'
test.cpp:26:   instantiated from here
/usr/include/g++-3/stl_iterator.h:574: no type named `iterator_category' in `struct iterator_traits<string>'

 

[edtdd]--Message édité par Krueger--[/edtdd]

Krueger Voilà mon programme:

Code :
  1. #include <iterator>
  2. #include <vector>
  3. template <class T> class A
  4. {
  5. public:
  6.   A();
  7.   A(const A &);
  8.   friend bool operator== <T> (const A<T> & a1, const A<T> & a2);
  9.   friend A<T> operator+  <T> (const A<T> & a1, const A<T> & a2);
  10. };
  11. template <class T> A<T>::A<T>()
  12. {}
  13. template <class T> A<T>::A<T>(const A<T> & a)
  14. {}
  15. template <class T> bool operator== (const A<T> & a1, const A<T> & a2)
  16. { return false;  }
  17. template <class T> A<T> operator+  (const A<T> & c1, const A<T> & c2)
  18. { return A<T>(); }
  19. int main()
  20. {
  21.   A<int> a1;
  22.   A<int> a2(a1);
  23.   if(a1 == a2)
  24.     a1 = a1 + a2;
  25.   return 0;
  26. }


 
Donc je n'arrive pas à faire compiler tout ça. Si je n'inclus aucune en-tête STL, ça passe. Sinon si j'enlève la surcharge de l'opérateur + de la classe A ou si je la définit dans la déclaration de la classe A, ça passe aussi. Moi pas comprendre. :/
 
Merci de votre aide.


Copyright © 1997-2025 Groupe LDLC (Signaler un contenu illicite / Données personnelles)