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

  FORUM HardWare.fr
  Programmation
  C++

  Héritage virtuelle Template

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Héritage virtuelle Template

n°2311557
xstf
Posté le 24-02-2018 à 00:39:57  profilanswer
 

Bonjour à tous,
 
Me remettant plus sérieusement au c++ j'ai expérimenté une classe que définirai les opérateur logique >, <, ==, ...
Il s'agit d'un expérimentation pour comprendre le principe et le concept.
 
La Méthode:  
 

Code :
  1. virtual int compare(T&t)=0;


 
dans la classe mère retourne la valeur  

  • -1  si  <  
  • 0   si  =
  • 1   si  >  


 
 
Les opérateurs sont défini comme suit.
 
Exemple:

Code :
  1. bool operator==(T&t){return(compare(t)==0);} ...


La classe fonctionne très bien quand elle est hérité  
 

Code :
  1. class Integer:public Logic<Integer>
  2. {...}


 
 
Voilà mon problème
 
Je me suis dit que ça serait bien de pouvoir comparer des types différents alors j'ai fait ceci.
 

Code :
  1. class Integer:public Logic<Integer>,public Logic<Float>
  2. {...}


 
Avant la compilation des erreur sont générés  

Citation :


IntelliSense: more than one operator "<" matches these operands:"Logic<T>::operator< [with T=Integer]" (ambiguous by inheritance) operand types are: Integer < Float  


 
Ainsi de suite pour chaque opérateurs définis...
 
 
 
Mais à la compilation tous fonctionne comme il se doit l'exécution est implacable. J'utilise le Microsoft Visual C++ 2013 Quand je transfère le code sur Arduino le premier cas fonctionne parfaitement mais le second cas ne fonctionne pas j'ai les messages d'erreurs suivants.
 

Citation :


error: request for member 'operator<' is ambiguous
note: candidates are: bool Logic<T>::operator<(T& ) [with T = Float]


 
 
 
 
Y-à-t'il une meilleur façon de faire pour que ce soit portable. Quelle est mon erreur. J'aimerai m'assurer qu'avant la compilation mon ide Visual c++ ne génère pas d'erreur et que ça fonctionne sur les deux plate-forme
 
 
 
Voici le code complet sur VC++:

Code :
  1. #include <iostream>
  2. using namespace std;
  3. template <class T>
  4. class Logic
  5. {
  6. public:
  7. Logic(){}
  8. virtual int compare(T &l) = 0;
  9. bool operator==(T &t){ return(compare(t) == 0); }
  10. bool operator!=(T &t){ return(compare(t) != 0); }
  11. bool operator<=(T &t){ return(compare(t) <= 0); }
  12. bool operator>=(T &t){ return(compare(t) >= 0); }
  13. bool operator<(T &t){ return(compare(t) < 0); }
  14. bool operator>(T &t){ return(compare(t) > 0); }
  15. };
  16. class Float;
  17. class Integer;
  18. class Float :virtual public Logic<Float>, virtual public Logic<Integer>
  19. {
  20. public:
  21. float value;
  22. Float(float v) :value(v){}
  23. int compare(Float &t);
  24. int compare(Integer &t);
  25. friend ostream& operator<<(ostream &tmp, Float &i){ return(tmp << i.value); }
  26. };
  27. class Integer :virtual public Logic<Integer>, virtual public Logic<Float>
  28. {
  29. public:
  30. int value;
  31. Integer(int v) :value(v){}
  32. int compare(Integer &t);
  33. int compare(Float &t);
  34. friend ostream& operator<<(ostream &tmp, Integer &i){ return(tmp << i.value); }
  35. };
  36. int Integer::compare(Integer &t)
  37. {
  38. int out = 0;
  39. if (value > t.value){ out = 1; }
  40. else if (value < t.value){ out = -1; }
  41. else out = 0;
  42. return(out);
  43. }
  44. int Integer::compare(Float &t)
  45. {
  46. int out = 0;
  47. if (value > t.value){ out = 1; }
  48. else if (value < t.value){ out = -1; }
  49. else out = 0;
  50. return(out);
  51. }
  52. int Float::compare(Float &t)
  53. {
  54. int out = 0;
  55. if (value > t.value){ out = 1; }
  56. else if (value < t.value){ out = -1; }
  57. else out = 0;
  58. return(out);
  59. }
  60. int Float::compare(Integer &t)
  61. {
  62. int out = 0;
  63. if (value > t.value){ out = 1; }
  64. else if (value < t.value){ out = -1; }
  65. else out = 0;
  66. return(out);
  67. }
  68. using namespace std;
  69. void main(void)
  70. {
  71. Integer i(21);
  72.  Float j(22);
  73. Integer k(23);
  74. cout << i << "<" << j << (i < j ? " TRUE " : " FALSE " ) << endl;
  75. cout << i << ">" << j << (i > j ? " TRUE " : " FALSE " ) << endl;
  76. cout << i << "=" << j << (i == j ? " TRUE " : " FALSE " ) << endl;
  77. cout << i << "==" << i << (i == i ? " TRUE " : " FALSE " ) << endl;
  78. cout << k << "<" << i << (k < i ? " TRUE " : " FALSE " ) << endl;
  79. cout << k << ">" << i << (k > i ? " TRUE " : " FALSE " ) << endl;
  80. cin.get();
  81. }


 
 
 
 
Code modifier pour le Arduino.
Fonctione mais quand je change les Héritages et un Integer pour un Float c'est la catastrophe.
Ça ne compile pas.
 

Code :
  1. namespace std
  2. class ostream{};
  3.   ostream cout;
  4.   const char*endl = "\n";
  5.   ostream& operator<<(ostream&t, const int &v){ Serial.print(v); return(t); }
  6.   ostream& operator<<(ostream&t, const float &v){ Serial.print(v); return(t); }
  7.   ostream& operator<<(ostream&t, const char *v){ Serial.print(v); return(t); }
  8. };
  9. using namespace std;
  10. template <class T>
  11. class Logic
  12. {public:
  13. virtual int compare(T &l) = 0;
  14. bool operator==(T &t){ return(compare(t) == 0); }
  15. bool operator!=(T &t){ return(compare(t) != 0); }
  16. bool operator<=(T &t){ return(compare(t) <= 0); }
  17. bool operator>=(T &t){ return(compare(t) >= 0); }
  18. bool operator<(T &t){ return(compare(t) < 0); }
  19. bool operator>(T &t){ return(compare(t) > 0); }
  20. };
  21. class Float;
  22. class Integer;
  23. class Float :public Logic<Float>//, public Logic<Integer>
  24. {
  25. protected:
  26. public:
  27.  
  28.   float value;
  29.   Float(float v) :value(v){}
  30.   int compare(Float &t);
  31.   int compare(Integer &t);
  32.   friend ostream& operator<<(ostream &tmp, Float &i){ return(tmp << i.value); }
  33. };
  34. class Integer :public Logic<Integer>//,public Logic<Float>
  35. {
  36.  
  37.  
  38.    public:
  39.  
  40.    int value;
  41.   Integer(int v) :value(v){}
  42.    
  43.   int compare(Integer &t);
  44.   int compare(Float &t);
  45.  
  46.  
  47.   friend ostream& operator<<(ostream &tmp, Integer &i){ return(tmp << i.value); }
  48. };
  49. int Integer::compare(Integer &t)
  50. {
  51.   int out = 0;
  52.   if (value > t.value){ out = 1; }
  53.   else if (value < t.value){ out = -1; }
  54.   else out = 0;
  55.   return(out);
  56. }
  57. int Integer::compare(Float &t)
  58. {
  59.   int out = 0;
  60.   if (value > t.value){ out = 1; }
  61.   else if (value < t.value){ out = -1; }
  62.   else out = 0;
  63.   return(out);
  64. }
  65. int Float::compare(Float &t)
  66. {
  67.   int out = 0;
  68.   if (value > t.value){ out = 1; }
  69.   else if (value < t.value){ out = -1; }
  70.   else out = 0;
  71.   return(out);
  72. }
  73. int Float::compare(Integer &t)
  74. {
  75.   int out = 0;
  76.   if (value > t.value){ out = 1; }
  77.   else if (value < t.value){ out = -1; }
  78.   else out = 0;
  79.   return(out);
  80. }
  81. void setup() {
  82. Serial.begin(74880);
  83.   Integer i(21);
  84.   Integer j(22);
  85.   //Float j(22);
  86.   Integer k(23);
  87.  
  88.   cout << i << "<" << j << (i < j ? " TRUE " : " FALSE " ) << endl;
  89.   cout << i << ">" << j << (i > j ? " TRUE " : " FALSE " ) << endl;
  90.   cout << i << "=" << j << (i == j ? " TRUE " : " FALSE " ) << endl;
  91.   cout << i << "==" << i << (i == i ? " TRUE " : " FALSE " ) << endl;
  92.   cout << k << "<" << i << (k < i ? " TRUE " : " FALSE " ) << endl;
  93.   cout << k << ">" << i << (k > i ? " TRUE " : " FALSE " ) << endl;
  94.  
  95.  
  96. }
  97. void loop() {}


 
 
Merci à l'avance.


Message édité par xstf le 24-02-2018 à 03:15:42
mood
Publicité
Posté le 24-02-2018 à 00:39:57  profilanswer
 


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C++

  Héritage virtuelle Template

 

Sujets relatifs
Exporter une BDD machine virtuelle/physiqueHéritage CSS
Quel language pour TemplateMembre object statique dans classe template : souci
[PowerShell] Lister rep. dont l'héritage des Permis. NTFS est bloquéVariadic template [résolu]
php template rtf en arabeun emulateur console virtuelle pour des jeux universels.
Problème à l'installation d'un template JoomlaCreation d'un nouveau process sans héritage d'environnement
Plus de sujets relatifs à : Héritage virtuelle Template


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR