Le probleme se trouve cette fois dans liste :: operator+(const maillon<T>)
g++ me dit
liste.cc: In member function ‘liste<T>& liste<T>::operator+(const maillon<T>&) [with T = int]’:
Client.cc:49: instantiated from here
liste.cc:206: erreur: passing ‘const maillon<int>’ as ‘this’ argument of ‘T& maillon<T>::valeur() [with T = int]’ discards qualifiers
Voici le code :
Le liste.h :
mon liste.cc :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
57
58
59 #include <stdio.h> #include <stdlib.h> #include <math.h> #include <iostream.h> // Classe T class T { }; // Classe maillon template <class T> class maillon { // Champs prives private: T data ; maillon<T> *suivant ; // Forme canonique de Coplien public: maillon(); maillon(const maillon<T>&) ; maillon(const T); ~maillon() ; maillon<T>& operator=(const maillon<T>&) ; maillon<T>& operator=(const T); // Autres methodes et operateurs maillon<T>* gonext(); T& valeur(); void affichemaillon(); }; // Classe liste template <class T> class liste { // Champs prives private: maillon<T>* tete; maillon<T>* fin ; // Forme canonique de Coplien public: liste() ; liste(const liste<T>&) ; ~liste() ; // Ajout de maillons liste<T>& operator+ (const T) ; //ajoute en fin de liste un elemnt liste<T>& operator+ (const maillon<T>&) ; //ajoute en fin de liste // Entrees-sorties void affiche () const ; // Fonction constante qui ne peut pas modifier les champs de la classe // Autres methodes et operateurs int longueur(); };
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 #include <stdio.h> #include <stdlib.h> #include <iostream.h> #include "liste.h" template <class T> maillon<T> :: maillon () { data = 0; suivant = NULL; } template <class T> maillon<T> :: maillon (const maillon<T>& m) { data = m.data; suivant = NULL; } template <class T> maillon<T> :: maillon (const T valeur) { data = valeur; suivant = NULL; } template <class T> maillon<T>* maillon<T> :: gonext() { return suivant; } template <class T> T& maillon<T> :: valeur() { return data; } template <class T> liste<T> :: liste() { tete = NULL ; fin = NULL ; } template <class T> liste<T>& liste<T> :: operator+ (const T valeur) { maillon<T>* parcours; maillon<T>* tmp; if (longueur() == 0) { tete = new maillon<T> (valeur); parcours = tete; } else { parcours = tete; while ((*parcours).gonext() != NULL) parcours = (*parcours).gonext(); tmp = new maillon<T> (valeur); (*parcours).modifpointeur(*tmp); } fin= parcours; return *this; } template <class T> liste<T>& liste<T> :: operator+ (const maillon<T>& m) { *this.operator+(m.valeur()); return *this; }
et mon client.cc :
Les methodes maillons marchent , la fonction liste :: operator+(T) marche
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 #include <stdio.h> #include <stdlib.h> #include <iostream> #include "liste.h" #include "liste.cc" int main(int argc, char **argv) { liste<int> l1; l1 + m1 ; // m1 et un maillon deja cree et teste }
Alors je ne comprends pas :
la fonction T maillon :: valeur() renvoie une valeur de type T et si je la passe en argument ds ma fonction operator+(T) ca devrait marcher non ???
Merci
Partager