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

  FORUM HardWare.fr
  Programmation
  C++

  std::sort et operator d'affectation

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

std::sort et operator d'affectation

n°1433494
Le_Berurie​r
Posté le 30-08-2006 à 08:59:21  profilanswer
 

Bonjour,
 
J'ai un petit problème de compilation.
Si quelqu'un pouvait m'aider.
 
J'ai une classe A ou je définis les operateurs =, <, >, <=, >=, ==
Dans une autre classe B, je définis un vecteur de A,

Code :
  1. std::vector<A> vect;


Le problème se trouve quand j'utilise la fonction de tri de <algorithm> :

Code :
  1. std::sort(vect.begin(), vect.end());


J'obtiens alors le message d'erreur suivant :
 

Code :
  1. ------ Build started: Project: MainEngine, Configuration: Debug Win32 ------
  2. Compiling...
  3. main.cpp
  4. d:\appli\microsoft visual studio 8\vc\include\algorithm(2623) : error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const A' (or there is no acceptable conversion)
  5.         A.h(14): could be 'A &A::operator =(A)'
  6.         while trying to match the argument list '(const A, A)'
  7.         d:\appli\microsoft visual studio 8\vc\include\algorithm(2639) : see reference to function template instantiation 'void std::_Insertion_sort1<_BidIt,A>(_BidIt,_BidIt,_Ty *)' being compiled
  8.         with
  9.         [
  10.             _BidIt=std::_Vector_const_iterator<A,std::allocator<A>>,
  11.             _Ty=A
  12.         ]
  13.         d:\appli\microsoft visual studio 8\vc\include\algorithm(2747) : see reference to function template instantiation 'void std::_Insertion_sort<_RanIt>(_BidIt,_BidIt)' being compiled
  14.         with
  15.         [
  16.             _RanIt=std::_Vector_const_iterator<A,std::allocator<A>>,
  17.             _BidIt=std::_Vector_const_iterator<A,std::allocator<A>>
  18.         ]
  19.         d:\appli\microsoft visual studio 8\vc\include\algorithm(2754) : see reference to function template instantiation 'void std::_Sort<std::_Vector_const_iterator<_Ty,_Alloc>,__w64 int>(_RanIt,_RanIt,_Diff)' being compiled
  20.         with
  21.         [
  22.             _Ty=A,
  23.             _Alloc=std::allocator<A>,
  24.             _RanIt=std::_Vector_const_iterator<A,std::allocator<A>>,
  25.             _Diff=__w64 int
  26.         ]
  27.         main.cpp(24) : see reference to function template instantiation 'void std::sort<std::_Vector_const_iterator<_Ty,_Alloc>>(_RanIt,_RanIt)' being compiled
  28.         with
  29.         [
  30.             _Ty=A,
  31.             _Alloc=std::allocator<A>,
  32.             _RanIt=std::_Vector_const_iterator<A,std::allocator<A>>
  33.         ]
  34. d:\appli\microsoft visual studio 8\vc\include\algorithm(2630) : error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const A' (or there is no acceptable conversion)
  35.         A.h(14): could be 'A &A::operator =(A)'
  36.         while trying to match the argument list '(const A, const A)'
  37. d:\appli\microsoft visual studio 8\vc\include\algorithm(2631) : error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const A' (or there is no acceptable conversion)
  38.         A.h(14): could be 'A &A::operator =(A)'
  39.         while trying to match the argument list '(const A, A)'
  40. Build log was saved at "file://BuildLog.htm"
  41. MainEngine - 3 error(s), 0 warning(s)
  42. ======== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


 
J'ai bien défini l'operator=
 

Code :
  1. A& operator = (const A& a);


 
Je comprends pas pourquoi le compilo attend 2 arguments  (const A& lhs, A rhs) ???
 
 
Merci de votre aide.
 

mood
Publicité
Posté le 30-08-2006 à 08:59:21  profilanswer
 

n°1433601
Taz
bisounours-codeur
Posté le 30-08-2006 à 11:17:49  profilanswer
 

pi il est cool ce message du compilo, on voit même pas le code incriminé.
 
essaie avec operator= synthétisé par le compilo déjà pour voir.

n°1433609
Le_Berurie​r
Posté le 30-08-2006 à 11:27:50  profilanswer
 

C'est ce que j'ai essayé de faire mais c'est pas possible  :( , ça doit forcément être comme ça l'opérateur d'affectation :

Code :
  1. A& operator = (const A& a);


Le compilateur ne veut pas non de cette signature : :non:

Code :
  1. A& operator = (const A& lhs, A& rhs);

Message cité 1 fois
Message édité par Le_Berurier le 30-08-2006 à 11:58:11
n°1433809
Taz
bisounours-codeur
Posté le 30-08-2006 à 16:47:58  profilanswer
 

Le_Berurier a écrit :

C'est ce que j'ai essayé de faire mais c'est pas possible  :(

je serais curieux de voir ça ...

n°1433963
Le_Berurie​r
Posté le 30-08-2006 à 20:42:13  profilanswer
 

Bon j'ai trouvé ce que c'était. J'ai un peu honte :)
Dans la fonction qui exécutait :
 
std::sort(vect.begin(), vect.end());
 
j'avais mis vect const :-/
Comme quoi à trop vouloir mettre des const partout...
 
Merci pour ton aide Taz.


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

  std::sort et operator d'affectation

 

Sujets relatifs
AWK|SORT en environnement WindowAffectation de plusieurs variables dans une seule.
[C++/résolu] operator [][] () et types variantsVariable qui sort pas de l'include
Operator= et membres constantsPb d'affectation d'un gridLayout d'une class a une autre
problème d'affectation de style dans mes formulaireerror C2274: '->' : illegal as right side of '.' operator
Collections sort(list lst, comparator c)[XSL:sort] select="variable" = rien
Plus de sujets relatifs à : std::sort et operator d'affectation


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