Bonjour,

J'ai représenté des Vers et des Strophes dans 2 classes que voici:


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
 
#ifndef vers_h
#define vers_h
#include <iostream>
#include <string>
using namespace std;
class Vers {
 
    private:
        string suiteMots;
        string rime;
 
    public:
        Vers();
        Vers(string s);
        Vers(string s, string r);
        virtual ~Vers();
        virtual string getSuiteMots() const;
        virtual void setSuiteMots(string sm);
        virtual string getRime() const;
        virtual void setRime(string r);
        virtual void saisie(istream& is);
        void affiche(ostream& os) const;
};
 
ostream& operator<<(ostream& os, const Vers& v);
istream& operator>>(istream& is, Vers& v);
#endif
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
#include <iostream>
#include <string>
using namespace std;
#include "Vers.h"
#include <limits>
Vers::Vers(){}
Vers::Vers(string sm){ suiteMots=sm;}
Vers::Vers(string sm, string r) { suiteMots=sm; rime= r;}
Vers::~Vers(){}
 
string Vers::getSuiteMots()const { return suiteMots;}
void Vers::setSuiteMots(string sm) { suiteMots=sm; }
string Vers::getRime()const {return rime;}
void Vers::setRime(string r) {rime=r;}
 
void Vers::saisie(istream& is) {
    cout << "Entrez le vers puis la rime." << endl;
    cin.ignore(200,'\n');
    getline(is,suiteMots);
    is >> rime;
}
 
void Vers::affiche(ostream& os) const { os << "<<" << suiteMots << ">>";}
 
ostream& operator<<(ostream& os, const Vers& v) {
    v.affiche(os);
    return os;
}
 
istream& operator>>(istream& is, Vers& v) {
    v.saisie(is);
    return is;
}
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
 
#ifndef strophe_h
#define strophe_h
 
class Strophe {
 
    private:
        Vers** suiteVers;
        int nbVers;
 
    public:
        Strophe();
        virtual ~Strophe();
        Strophe(const Strophe&);
        Strophe& operator=(const Strophe&);
        virtual void saisie(istream& is);
        virtual Vers* vers(int i) const;
        Vers* operator[](int i) const;
        virtual void affiche(ostream& os) const;
};
 
ostream& operator<<(ostream& os, const Strophe& s);
istream& operator>>(istream& is, Strophe& s);
 
 
#endif
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
#include <iostream>
#include <string>
using namespace std;
#include "Vers.h"
#include "Strophe.h"
 
Strophe::Strophe(){suiteVers=NULL; nbVers=0;}
 
Strophe::~Strophe(){if (suiteVers) delete[] suiteVers;}
 
Strophe::Strophe(const Strophe& s) {
    this->nbVers = s.nbVers;
    this->suiteVers = new Vers*[s.nbVers];
    for (int i=0 ; i<this->nbVers ; i++) {
        this->suiteVers[i] = s.suiteVers[i];
    }
}
 
Strophe& Strophe::operator=(const Strophe& s) {
    if (this != &s) {
        this->nbVers = s.nbVers;
        if (this->suiteVers) {
            delete[] this->suiteVers;
        }
        this->suiteVers = new Vers*[s.nbVers];
        for (int i=0 ; i<this->nbVers ; i++) {
            this->suiteVers[i] = s.suiteVers[i];
        }
    }
    return *this;
}
 
Vers* Strophe::vers(int i) const {
    if (i>=0 && i<nbVers) return suiteVers[i];
    else return NULL;
}
 
Vers* Strophe::operator[](int i) const {
    return suiteVers[i];
}
 
 
void Strophe::saisie(istream& is) {
    if (suiteVers) delete[] suiteVers;
    cout << "Entrer le nombre de vers : " << endl;
    is >> nbVers;
    suiteVers = new Vers*[nbVers];
    for (int i=0; i<nbVers; i++) {
        Vers *v = new Vers();
        v->saisie(is);
        suiteVers[i] = v;
    }
}
 
void Strophe::affiche(ostream& os) const {
    for (int i=0; i<nbVers; i++) {
        suiteVers[i]->affiche(os); os << endl;
    }
}
 
ostream& operator<<(ostream& os, const Strophe& s) {
    s.affiche(os);
    return os;
}
 
istream& operator>>(istream& is, Strophe& s) {
    s.saisie(is);
    return is;
}
Mon problème est que je souhaite surcharger mon opérateur [] afin de pouvoir écrire quelque chose comme ceci dans mon main():

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
int main () {
    Strophe S;
    cin >> S;
    cout << *S[1]; // affiche le second vers
    S[2] = new Vers("J'ai une jolie trousse","ousse"); // modifie le 3eme vers
 
    return 0;
}
J'ai donc écrit cette méthode de surcharge pour l'opérateur []:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
Vers* Strophe::operator[](int i) const {
    return suiteVers[i];
}
Pas de souci pour l'affichage d'un vers.
Par contre pour la modification, j'ai une erreur de compilation.
Poesies\main.cpp|13|error: lvalue required as left operand of assignment|
J'ai beau faire des schémas...



...mais je ne comprends toujours pas pourquoi cette erreur sur la modification.
Ma méthode retourne pourtant bien un pointeur...S[2] dans mon exemple est un pointeur, donc pourquoi cette erreur?

Merci a ce qui m'ont lu jusqu'au bout et qui me répondront.