problème avec un construteurde copie
bonjour, j'ai un petit soucie avec ma compilation et je ne voix pas du tout mon erreur, voici se que j'ai;
main.cpp: In function ‘int main()’:
main.cpp:34: erreur: no match for ‘operator+=’ in ‘langues += anglais’
cnotes.h:30: note: candidats sont: CNotes& CNotes::operator+=(double)
voici mon code.
cnotes.h
Code:
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 46 47 48 49 50 51 52 53 54 55 56
| #ifndef CNOTES_H
#define CNOTES_H
#include <iostream>
#include <string>
using namespace std ;
class CElem ;
typedef CElem* pElem ;
class CElem {
public :
double note ;
pElem suiv ;
} ;
class CNotes {
pElem prem , dern ;
int nbElem ;
public :
CNotes(double n = -1 )
: prem(NULL) , dern(NULL) , nbElem(0) {
if ( n != -1 ) Ajoute(n) ;
}
~CNotes() ;
int nbNotes() const { return nbElem ; }
void Ajoute(double v ) ;
void Ajoute( CNotes& n ) const;
double Moyenne() const ;
CNotes& operator +=(double v) {
Ajoute(v);
return *this;
}
CNotes& operator + (double v){
CNotes& toto(*this);
toto.Ajoute(v);
return toto;
}
double& operator [](int index){
while ( nbElem <= index) Ajoute(0);
pElem p= prem;
while( index--) p = p->suiv;
return p->note;
}
/*CNotes& operator +=(const CNotes& n ){
}*/
} ;
#endif |
cnotes.cpp
Code:
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
| #include "cnotes.h"
CNotes::~CNotes()
{
pElem p = prem ;
while ( prem != NULL ) {
p = p->suiv ; delete prem ; prem = p ;
}
prem = dern = NULL ;
nbElem = 0 ;
}
void CNotes::Ajoute(double v )
{
pElem p = new CElem ; //pointeur de maillon
p->note = v ; p->suiv = NULL ;
if ( prem == NULL ) prem = dern = p ;
else {
dern->suiv = p ; dern = dern->suiv ;
}
nbElem++ ;
}
double CNotes::Moyenne() const
{
int i;
pElem p = prem;
double total;
for (i = 0; i < nbNotes() ;i++){
total += p ->note;
p = p -> suiv;
}
return total/nbElem ;
}
void CNotes::Ajoute(const CNotes& n){
pElem p = n.prem;
while (p != NULL){
Ajoute(p->note);
p = p ->suiv;
}
} |
main.cpp
Code:
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
| #include "cnotes.h"
void affiche(const string& m, const CNotes& c )
{
cout << m << " : " << c.nbNotes() << " note" ;
if ( c.nbNotes() > 1 ) cout << 's' ;
cout << ", moy. = " << c.Moyenne() << endl ;
}
int main(void)
{
CNotes math ; // constructeur par défaut
math.Ajoute(12) ; // méthode Ajoute()
math.Ajoute(10) ;
affiche("math ", math ) ;
CNotes physique(7) ; // constructeur avec argument
physique += 13 ; // opérateur += avec arg. double
physique += 10 ;
affiche("physique", physique ) ;
CNotes anglais ;
anglais[0] = 11 ; // opérateur d'accès direct []
anglais[1] = 15 ;
anglais = anglais + 13 ; // opérateur + avec arg. double
// op. = sur même objet CNotes
CNotes francais(12) ;
francais[2] = 14 ; // devoir d'indice 1 non rendu !
affiche("anglais ", anglais ) ;
affiche("francais", francais ) ;
CNotes langues = francais ; // constructeur de copie
langues += anglais ; // opérateur += avec arg. CNotes
affiche("langues ", langues ) ;
/*
CNotes bilan ;
bilan = math + physique ; // opérateur + avec arg. CNotes
// op. = avec autre objet CNotes
bilan += anglais ; // opérateur += avec arg. CNotes
bilan += francais ;
affiche("bilan ", bilan ) ;
*/
} |
bonne soirée a vous