Bonjour,
Alors voila j'ai surchargé l'opérateur + en tant que fonction ami et j'ai une erreur lors de l'execution du programme, du typesur certain pc et de type
Code : Sélectionner tout - Visualiser dans une fenêtre à part Erreur de segmentationAbandon
Code : Sélectionner tout - Visualiser dans une fenêtre à part *** glibc detected *** free(): invalid next size (fast): 0x0804b078 ***
Donc je m'explique : j'ai deux fichiers, un fichier chaine.h qui définit une classe Chain avec prototypes des fonctions et un fichier chaine.cpp qui contient le main et la définition des fonctions.
Pour mieux illustrer, voici le code :
chaine.h :
chaine.cpp :
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 class Chaine{ // prototypes fonctions amies friend bool operator == (Chaine A, Chaine B); friend char * operator + (Chaine A, Chaine B); private: char *ch; public: // prototypes et/ou definitions des fonctions membres inline Chaine(); inline Chaine(char car); inline Chaine(char c[]); inline Chaine(Chaine &P); inline ~Chaine(); inline int taille() const; inline void Afficher(); inline char operator [] (int indice) const; inline char & operator [] (int indice); inline char * operator = (const Chaine &P); inline char * operator += (const Chaine &P); inline Chaine & operator () (int indice, int len); };
J'avais déjà réalisé, comme vous pourrez le constater, quelques surcharges d'opérateurs en tant que fonctions membres (certaines consultaient seulement d'autres modifiaient).
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 #include <iostream> #include <cmath> #include "chaine.h" using namespace std; int main() { Chaine c1("Bonjour"); Chaine c2(c1); c1.Afficher(); cout << c1.taille() << endl; cout << c1[3] << endl; Chaine c3("abc"); c3 = c1; c3.Afficher(); c3 = "abc"; c1 += c3; c1.Afficher(); Chaine cc("Bonjour"); Chaine c4 = cc(2,3); c4.Afficher(); // ERREUR ICI DE MA SURCHARGE DE L'OPERATEUR + cout << c1+c2 << endl; if (c1 == cc) { cout << "Les deux chaines sont identiques" << endl; }else{ cout << "Les deux chaines sont différentes" << endl; } } inline Chaine::Chaine() { ch = 0; } inline Chaine::Chaine(char car) { ch = new char[1]; ch[0]=car; } inline Chaine::Chaine(char c[]) { ch=new char[strlen(c)+1]; strcpy(ch,c); } inline Chaine::Chaine(Chaine &P) { ch=new char[strlen(P.ch)+1]; strcpy(ch,P.ch); } inline Chaine::~Chaine() { if (ch) { delete []ch; } } inline void Chaine::Afficher() { cout << ch << endl; } inline int Chaine::taille() const { return strlen(ch); } inline char Chaine::operator [] (int indice) const{ return ch[indice]; } inline char & Chaine::operator [] (int indice) { return ch[indice]; } inline char * Chaine::operator = (const Chaine &P) { //ch=new char[strlen(P.ch)+1]; return strcpy(ch,P.ch); } inline char * Chaine::operator += (const Chaine &P) { return strcat(ch,P.ch); } inline Chaine & Chaine::operator () (int indice, int len) { char *c = new char[len+1]; for (int i = 0;i < len; i++) { c += this->ch[indice+i]; } cout << c; return *this; } bool operator == (Chaine A, Chaine B) { if (strcmp(A.ch,B.ch) == 0) { return true; }else{ return false; } } // SURCHARGE D'OPERATEUR QUI POSE PROBLEME char * operator + (Chaine A, Chaine B) { return (strcat(A.ch,B.ch)); }
Ensuite je me suis mis au surcharge d'opérateur en fonction ami, j'ai réussi à réaliser celle qui consulte (opérateur ==) par contre j'ai des problèmes pour celle qui modifie (opérateur +).
Merci de l'aide que vous voudrez bien m'apporter
Andréas
Partager