Problème avec surcharge de l'opérateur []
Bonjour,
Alors voila j'ai un problème lors de la surcharge de l'opérateur [] dont l'effet est la modification je précise bien, car la surcharge de cet opérateur en consultation je l'ai faite et elle marche.
J'ai une classe Chaine dans un fichier chaine.h et à côté mon fichier chaine.cpp.
Lors de la compilation j'ai plusieurs lignes d'erreur qui sont :
Code:
1 2 3 4 5 6 7 8
| chaine.h:23: erreur: Chaine& Chaine::operator[](int) cannot be overloaded
chaine.h:22: erreur: with char Chaine::operator[](int)
chaine.cpp:54: erreur: prototype for Chaine& Chaine::operator[](int) does not match any in class Chaine
chaine.cpp:50: erreur: candidate is: char Chaine::operator[](int)
chaine.cpp:54: erreur: Chaine& Chaine::operator[](int) cannot be overloaded
chaine.cpp:50: erreur: with char Chaine::operator[](int)
chaine.cpp: In member function Chaine& Chaine::operator[](int):
chaine.cpp:55: erreur: invalid initialization of reference of type Chaine& from expression of type char |
J'ai essayé de définir comme constant ma surcharge d'opérateur, et lorsque je fais cela, il ne subsiste que cette erreur là :
Code:
1 2
| chaine.cpp: In member function Chaine& Chaine::operator[](int) const:
chaine.cpp:55: erreur: invalid initialization of reference of type Chaine& from expression of type char |
Voici mon code pour mieux illustrer le problème :
chaine.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
| class Chaine{
// prototypes fonctions amies
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);
inline Chaine & operator [] (int indice) const; // SURCHARGE QUI POSE PROBLEME
}; |
chaine.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 46 47 48 49 50 51 52 53 54 55 56 57
| #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;
}
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(ch)+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) {
return ch[indice];
}
// SURCHARGE QUI POSE PROBLEME
inline Chaine & Chaine::operator [] (int indice) const{
return ch[indice];
} |
J'ai une petite idée d'où viendrait le problème, je pense que c'est dans le type de retour de la surcharge qui est de type char et non de type Chaine. Mais je ne vois pas comment faire autrement.
Je vous remercie de l'aide que vous voudrez bien m'apporter ;)
Andréas
operator[] (quel_type_ici)
Citation:
Envoyé par
Laurent Gomila
Et à moins que tu ne supportes des opérations bizarres, le type des indices devrait au moins être non-signé.
Pourquoi?