IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 C++ Discussion :

Référence constante getter


Sujet :

C++

  1. #1
    maximesav1
    Invité(e)
    Par défaut Référence constante getter
    Rebonjour,

    Second exercice, second blocage...

    Voici le code :
    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
    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;
    }
    et voici le message d'erreur :

    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
     
    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;
            ~~~~~~~~~~^~~~~~~~~~
    Dans mon énoncé, il est précisé :
    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 !

  2. #2
    Rédacteur/Modérateur


    Homme Profil pro
    Network game programmer
    Inscrit en
    Juin 2010
    Messages
    7 146
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Canada

    Informations professionnelles :
    Activité : Network game programmer

    Informations forums :
    Inscription : Juin 2010
    Messages : 7 146
    Billets dans le blog
    4
    Par défaut
    Salut,

    voilà les lignes importantes de l'erreur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    /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:
    Comme il le dit lui-même : il ne sait pas réaliser une copie de vector<Ingredient> parce que tu ne fournis pas l'opérateur d'affectation et il ne sait pas le créer par défaut vu que tu utilises une référence constante comme membre.

    Btw, il n'est absolument pas nécessaire d'avoir un member en référence constante pour le retourner ainsi.
    Pensez à consulter la FAQ ou les cours et tutoriels de la section C++.
    Un peu de programmation réseau ?
    Aucune aide via MP ne sera dispensée. Merci d'utiliser les forums prévus à cet effet.

  3. #3
    maximesav1
    Invité(e)
    Par défaut
    Quand on parle d'opérateur d'affectation, il s'agit de ça ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
        Produit& operator=(const Produit& o) { 
       //quelque chose
        return *this; 
        }
    à mettre dans la classe produit ?

    Je suis complétement perdu !
    Dernière modification par maximesav1 ; 22/07/2016 à 10h57.

  4. #4
    maximesav1
    Invité(e)
    Par défaut
    Je sais pas trop si je peux up, je le fais, navré d'avance.. J'ai pas avancé d'un iota, j'arrive pas à créer un vecteur de recettes pour écrire les r.tostring à la suite..

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 9
    Dernier message: 27/05/2009, 10h20
  2. Ecriture étrange d'une référence constante
    Par ram-0000 dans le forum Débuter
    Réponses: 5
    Dernier message: 12/03/2009, 13h19
  3. itérer sur une référence constante de vector
    Par manonoc dans le forum SL & STL
    Réponses: 1
    Dernier message: 17/08/2008, 12h50
  4. renvois de référence constantes.
    Par castorus dans le forum C++
    Réponses: 6
    Dernier message: 04/06/2007, 17h35
  5. Réponses: 10
    Dernier message: 03/03/2005, 13h36

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo