Surcharger l'operateur + en c++

Résolu/Fermé
yuri648 Messages postés 677 Date d'inscription mardi 30 décembre 2008 Statut Membre Dernière intervention 20 mai 2015 - 21 oct. 2011 à 17:51
yuri648 Messages postés 677 Date d'inscription mardi 30 décembre 2008 Statut Membre Dernière intervention 20 mai 2015 - 21 oct. 2011 à 22:20
Bonjour,

je tente a surcharger l'operateur + en c++ mais je recois cette erreur

**** Build of configuration Debug for project test ****

make all
Building file: ../Vecteur3D.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Vecteur3D.d" -MT"Vecteur3D.d" -o"Vecteur3D.o" "../Vecteur3D.cpp"
../Vecteur3D.cpp:8:22: warning: extra tokens at end of #include directive [enabled by default]
Finished building: ../Vecteur3D.cpp

Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp"
../main.cpp:11:22: warning: extra tokens at end of #include directive [enabled by default]
../main.cpp: In function `int main()':
../main.cpp:19:9: error: no match for `operator+' in `vv + n'
../main.cpp:19:9: note: candidates are:
/usr/include/c++/4.6/bits/stl_iterator.h:327:5: note: template<class _Iterator> std::reverse_iterator<_Iterator> std::operator+(typename std::reverse_iterator<_Iterator>::difference_type, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/basic_string.h:2306:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.tcc:694:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.tcc:710:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(_CharT, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2343:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/include/c++/4.6/bits/basic_string.h:2359:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
make: *** [main.o] Error 1


voici la classe main
#include<iostream>

using namespace std;
#include"Vecteur3D.h";

int main()
{

Vecteur3D vv(23,3,3),n(44,5,7);
Vecteur3D hh;
//vv.affiche();
hh=vv + n;

//cout<<h.x<<endl;
return 0;
}

le fichier Vecteur3D.cpp


Vecteur3D::Vecteur3D()
{Vecteur3D& Vecteur3D:: operator +=(const Vecteur3D &a)
{
x+=a.x;
y+=a.y;
z+=a.z;;

return *this;
}
}
voici une parti du fichier Vecteur3D.h

Vecteur3D & operator +=(const Vecteur3D &a);

merci d'avance

1 réponse

KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
21 oct. 2011 à 18:26
Il me semble qu'il ne doit pas y avoir d'espace dans le nom de la méthode ::operator+=
De plus, je ne comprends pas pourquoi tu as mis ce code à l'intérieur du constructeur Vecteur3D::Vecteur3D() alors que ce sont deux choses différentes :

Vecteur3D::Vecteur3D()
{
}

Vecteur3D& Vecteur3D::operator+=(const Vecteur3D &a)
{
    x+=a.x;
    y+=a.y;
    z+=a.z;
    return *this;
}
1
yuri648 Messages postés 677 Date d'inscription mardi 30 décembre 2008 Statut Membre Dernière intervention 20 mai 2015 7
21 oct. 2011 à 21:19
merci de me repondre mais le probleme n'est pas résolu
0
KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
21 oct. 2011 à 21:36
Dans ton code tu fais hh = vv + n, or c'est l'opérateur += que tu as redéfinis.
Donc soit tu redéfinis operator+ soit tu utilises un code avec +=
0
yuri648 Messages postés 677 Date d'inscription mardi 30 décembre 2008 Statut Membre Dernière intervention 20 mai 2015 7
21 oct. 2011 à 22:20
c 'est vrai merci beacoup

j'ai fait n+=vv; elle a marché
0