Rebonjour,
Second exercice, second blocage...
Voici le code :
et voici le message d'erreur :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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192 #include <iostream> #include <vector> #include <sstream> using namespace std; /***************************************************** * Compléter le code à partir d'ici *****************************************************/ class Produit{ protected: string nom; string unite; public: Produit(string nom,string unite=""): nom(nom), unite(unite) {} //initialize and assignement's difference string getNom() const{ return nom; } string getUnite() const{ return unite; } virtual string toString() const{ return nom; } virtual const Produit* adapter(double n) const{ return this; } }; class Ingredient{ private: const Produit &p;//Changed to refernce double quantite; public: Ingredient(const Produit & p,double quantite): p(p),quantite(quantite) {} Produit const & getProduit() const{ return p; //return une reference?? } double getQuantite() const{ return quantite; } string descriptionAdaptee() const{ stringstream ss; const Produit* ptr = p.adapter(quantite); ss << quantite << " " << p.getUnite() << " de " << ptr->toString(); return ss.str(); } }; class Recette{ private: vector<Ingredient> lst; string nom; double nbFois_; public: Recette(string nom, double nbFois_ = 1.0): nom(nom), nbFois_(nbFois_) {} void ajouter( Produit &p, double quantite ){ Ingredient a(p,quantite*nbFois_); lst.push_back(a); } double quantiteTotale(string nom) const{ for (auto const i : lst){ if (i.getProduit().getNom() == nom){ return i.getQuantite(); } } return 0.0; } const Recette adapter(double n) const{ double newNbFois = nbFois_ * n; Recette x(nom, newNbFois); for (auto const i : lst){ const Produit & tmp = i.getProduit(); x.lst.push_back(Ingredient(tmp,i.getQuantite()*n));//get the thing the pointer points at. } return x; } string toString() const{ stringstream ss; ss << "Recette \"" << nom << "\" x " << nbFois_<< ":\n"; unsigned int i = 1; for (auto const & ingredient : lst){ ss << i << ". " << ingredient.descriptionAdaptee();//getProduit().toString(); if (i != lst.size()){ ss << endl; } i += 1; } string s = ss.str(); return s; } }; class ProduitCuisine: public Produit{ private: Recette r; public: ProduitCuisine(string nom): Produit(nom,"portion(s)"),r(nom) {} void ajouterARecette(Produit& produit, double quantite){ r.ajouter(produit,quantite); } const Produit* adapter(double n) const override{ ProduitCuisine *p = new ProduitCuisine(nom); p->r = r.adapter(n); return this; } virtual string toString() const override{ stringstream ss; ss << Produit::toString() << "\n" << r.toString(); return ss.str(); } }; /******************************************* * Ne rien modifier après cette ligne. *******************************************/ void afficherQuantiteTotale(const Recette& recette, const Produit& produit) { string nom = produit.getNom(); cout << "Cette recette contient " << recette.quantiteTotale(nom) << " " << produit.getUnite() << " de " << nom << endl; } int main() { // quelques produits de base Produit oeufs("oeufs"); Produit farine("farine", "grammes"); Produit beurre("beurre", "grammes"); Produit sucreGlace("sucre glace", "grammes"); Produit chocolatNoir("chocolat noir", "grammes"); Produit amandesMoulues("amandes moulues", "grammes"); Produit extraitAmandes("extrait d'amandes", "gouttes"); ProduitCuisine glacage("glaçage au chocolat"); // recette pour une portion de glaçage: glacage.ajouterARecette(chocolatNoir, 200); glacage.ajouterARecette(beurre, 25); glacage.ajouterARecette(sucreGlace, 100); cout << glacage.toString() << endl; ProduitCuisine glacageParfume("glaçage au chocolat parfumé"); // besoin de 1 portions de glaçage au chocolat et de 2 gouttes // d'extrait d'amandes pour 1 portion de glaçage parfumé glacageParfume.ajouterARecette(extraitAmandes, 2); glacageParfume.ajouterARecette(glacage, 1); cout << glacageParfume.toString() << endl; Recette recette("tourte glacée au chocolat"); recette.ajouter(oeufs, 5); recette.ajouter(farine, 150); recette.ajouter(beurre, 100); recette.ajouter(amandesMoulues, 50); recette.ajouter(glacageParfume, 2); cout << "=== Recette finale =====" << endl; cout << recette.toString() << endl; afficherQuantiteTotale(recette, beurre); cout << endl; // double recette Recette doubleRecette = recette.adapter(2); cout << "=== Recette finale x 2 ===" << endl; cout << doubleRecette.toString() << endl; afficherQuantiteTotale(doubleRecette, beurre); afficherQuantiteTotale(doubleRecette, oeufs); afficherQuantiteTotale(doubleRecette, extraitAmandes); afficherQuantiteTotale(doubleRecette, glacage); cout << endl; cout << "===========================\n" << endl; cout << "Vérification que le glaçage n'a pas été modifié :\n"; cout << glacage.toString() << endl; return 0; }
Dans mon énoncé, il est précisé :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 In file included from /usr/local/include/c++/6.1.0/bits/char_traits.h:39:0, from /usr/local/include/c++/6.1.0/ios:40, from /usr/local/include/c++/6.1.0/ostream:38, from /usr/local/include/c++/6.1.0/iostream:39, from main.cpp:1: /usr/local/include/c++/6.1.0/bits/stl_algobase.h: In instantiation of 'static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = const Ingredient*; _OI = Ingredient*]': /usr/local/include/c++/6.1.0/bits/stl_algobase.h:386:44: required from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = const Ingredient*; _OI = Ingredient*]' /usr/local/include/c++/6.1.0/bits/stl_algobase.h:422:45: required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = __gnu_cxx::__normal_iterator<const Ingredient*, std::vector<Ingredient> >; _OI = __gnu_cxx::__normal_iterator<Ingredient*, std::vector<Ingredient> >]' /usr/local/include/c++/6.1.0/bits/stl_algobase.h:455:8: required from '_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<const Ingredient*, std::vector<Ingredient> >; _OI = __gnu_cxx::__normal_iterator<Ingredient*, std::vector<Ingredient> >]' /usr/local/include/c++/6.1.0/bits/vector.tcc:206:31: required from 'std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = Ingredient; _Alloc = std::allocator<Ingredient>]' main.cpp:55:7: required from here /usr/local/include/c++/6.1.0/bits/stl_algobase.h:324:18: error: use of deleted function 'Ingredient& Ingredient::operator=(const Ingredient&)' *__result = *__first; ~~~~~~~~~~^~~~~~~~~~ main.cpp:31:7: note: 'Ingredient& Ingredient::operator=(const Ingredient&)' is implicitly deleted because the default definition would be ill-formed: class Ingredient{ ^~~~~~~~~~ main.cpp:31:7: error: non-static reference member 'const Produit& Ingredient::p', can't use default assignment operator In file included from /usr/local/include/c++/6.1.0/bits/char_traits.h:39:0, from /usr/local/include/c++/6.1.0/ios:40, from /usr/local/include/c++/6.1.0/ostream:38, from /usr/local/include/c++/6.1.0/iostream:39, from main.cpp:1: /usr/local/include/c++/6.1.0/bits/stl_algobase.h: In instantiation of 'static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = Ingredient*; _OI = Ingredient*]': /usr/local/include/c++/6.1.0/bits/stl_algobase.h:386:44: required from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = Ingredient*; _OI = Ingredient*]' /usr/local/include/c++/6.1.0/bits/stl_algobase.h:422:45: required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = Ingredient*; _OI = Ingredient*]' /usr/local/include/c++/6.1.0/bits/stl_algobase.h:455:8: required from '_OI std::copy(_II, _II, _OI) [with _II = Ingredient*; _OI = Ingredient*]' /usr/local/include/c++/6.1.0/bits/vector.tcc:211:17: required from 'std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = Ingredient; _Alloc = std::allocator<Ingredient>]' main.cpp:55:7: required from here /usr/local/include/c++/6.1.0/bits/stl_algobase.h:324:18: error: use of deleted function 'Ingredient& Ingredient::operator=(const Ingredient&)' *__result = *__first; ~~~~~~~~~~^~~~~~~~~~
getProduit doit retourner une référence constante au produit
Je pense que je me mélange les pinceaux dans la syntaxe.
Merci infiniment à ceux qui m'aideront !