Bonjour.
Je suis parti de cette video :
où je tente de pousser l'exercice un peu plus loin en surchargeant l'opérateur "<<" agissant sur les "struct Comment" pour faire un effet de mise en page et je rencontre un soucis dont je ne trouve la cause !
Je déclare une struct Comment dont les instances prennent 2 paramètre (1 string et 1 int optionnel).
Ensuite, je surcharge l'opérateur << qui est supposé afficher le commentaire avec un petit effet de mise en page.
Le code suivant fonctionne :
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 #include <iostream> #include <list> #include <string> using namespace std; struct Comment { string Str; int Symbol; Comment(const string &str, const int &symbol= 2) { Str = str; Symbol = symbol; } }; ostream &operator<<(ostream &COUT, Comment &comment) { int size = comment.Str.size(); for (int i = 0; i < size; i++) { COUT << comment.Symbol; } COUT << endl << comment.Str << endl; for (int i = 0; i < size; i++) { COUT << comment.Symbol; } return COUT; } struct YoutubeChannel { string Name; int SubscribersCount; YoutubeChannel(string name, int subscribersCount) { Name = name; SubscribersCount = subscribersCount; } bool operator==(const YoutubeChannel channel) const { return this->Name == channel.Name; } }; ostream &operator<<(ostream &COUT, YoutubeChannel &ytChannel) { COUT << "Name: " << ytChannel.Name << endl; COUT << "SubscribersCount: " << ytChannel.SubscribersCount << endl; ; return COUT; } struct MyCollection { list<YoutubeChannel> myChannels; void operator+=(YoutubeChannel &channel) { this->myChannels.push_back(channel); } void operator-=(YoutubeChannel &channel) { this->myChannels.remove(channel); } }; ostream &operator<<(ostream &COUT, MyCollection &myCollection) { for (YoutubeChannel ytChannel : myCollection.myChannels) { COUT << ytChannel << endl; } return COUT; } int main(void) { YoutubeChannel yt1("CodeBeauty", 75000); YoutubeChannel yt2("Second channel", 70); YoutubeChannel yt3("third channel", 0); MyCollection myCollection; myCollection += yt1; myCollection += yt3; myCollection += yt2; cout << myCollection; myCollection -= yt1; Comment comment("Printing after removing a channel", 9); cout << comment; cout << endl; cout << myCollection; return 0; }
Par contre celui-ci refuse de passer au compilateur alors que je ne tente que de remplacer mon paramètre optionnel "Symbol" int par un char :
Je ne comprends absolument pas pourquoi cela refuse de fonctionner .
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 #include <iostream> #include <list> #include <string> using namespace std; struct Comment { string Str; char Symbol; Comment(const string &str, const char &symbol= '&') { Str = str; Symbol = symbol; } }; ostream &operator<<(ostream &COUT, Comment &comment) { int size = comment.Str.size(); for (int i = 0; i < size; i++) { COUT << comment.Symbol; } COUT << endl << comment.Str << endl; for (int i = 0; i < size; i++) { COUT << comment.Symbol; } return COUT; } struct YoutubeChannel { string Name; int SubscribersCount; YoutubeChannel(string name, int subscribersCount) { Name = name; SubscribersCount = subscribersCount; } bool operator==(const YoutubeChannel channel) const { return this->Name == channel.Name; } }; ostream &operator<<(ostream &COUT, YoutubeChannel &ytChannel) { COUT << "Name: " << ytChannel.Name << endl; COUT << "SubscribersCount: " << ytChannel.SubscribersCount << endl; ; return COUT; } struct MyCollection { list<YoutubeChannel> myChannels; void operator+=(YoutubeChannel &channel) { this->myChannels.push_back(channel); } void operator-=(YoutubeChannel &channel) { this->myChannels.remove(channel); } }; ostream &operator<<(ostream &COUT, MyCollection &myCollection) { for (YoutubeChannel ytChannel : myCollection.myChannels) { COUT << ytChannel << endl; } return COUT; } int main(void) { YoutubeChannel yt1("CodeBeauty", 75000); YoutubeChannel yt2("Second channel", 70); YoutubeChannel yt3("third channel", 0); MyCollection myCollection; myCollection += yt1; myCollection += yt3; myCollection += yt2; cout << myCollection; myCollection -= yt1; Comment comment("Printing after removing a channel", 'F'); cout << comment; cout << endl; cout << myCollection; return 0; }
Quelqu'un pourrait il m'aiguiller sur mon erreur ou bien pourquoi cela n'est pas sensé fonctionner ?
Partager