does not remove the contents list



  • There's this class. When main I'm trying to get the contents out. listmakes a mistake:

    "list iterators incompatible."

    The mistake is in these lines:

    for (list <string>::iterator k = a[i].getsIngridients().begin(); k != a[i].getsIngridients().end(); k++) {
                cout << *k << endl;
            }
    

    dish.h

     class dish {
    private:
        string sDishName;
        list <string> sIngridients;
        double dPrice;
        double dPrepareTime;
    public:
        DISHDLL_API dish();
        DISHDLL_API dish(string name, list<string> ingr, double price, double time);
        DISHDLL_API ~dish();
        DISHDLL_API void addDish(dish &newDish, vector <dish> &allDishes);
        DISHDLL_API void delDish(int index, vector <dish> &allDishes);
        //get funcs
        DISHDLL_API string getsDishName();
        DISHDLL_API list<string> getsIngridients();
        DISHDLL_API double getdPrice();
        DISHDLL_API double getdPrepareTime();
    };
    

    dish.cpp

    #include "stdafx.h"
    #include "Dish.h"
    

    dish::dish() {

    }

    dish::dish(string name, list<string> ingr, double price, double time) {
    sDishName = name;
    sIngridients = ingr;
    dPrice = price;
    dPrepareTime = time;
    }

    dish::~dish() {

    }

    void dish::addDish(dish &newDish, vector<dish> &allDishes) {
    allDishes.push_back(newDish);
    }

    void dish::delDish(int index, vector<dish> &allDishes) {
    allDishes.erase(allDishes.begin() + index);
    }

    string dish::getsDishName() {
    return sDishName;
    }

    list<string> dish::getsIngridients() {
    return sIngridients;
    }

    double dish::getdPrice() {
    return dPrice;
    }

    double dish::getdPrepareTime() {
    return dPrepareTime;
    }

    main.cpp

    #include "stdafx.h"
    #include "Dish.h"
    using namespace std;

    int main()
    {
    vector<dish> a;
    string str = "name";
    list<string> list1;
    list1.push_back(str);
    list1.push_back(str);
    double price = 5.1;
    double time = 10.5;

    dish b(str, list1, price, time);
    
    b.addDish(b, a);
    b.addDish(b, a);
    b.addDish(b, a);
    
    for (int i = 0; i &lt; a.size(); i++) {
        cout &lt;&lt; a[i].getsDishName() &lt;&lt; endl;
        cout &lt;&lt; a[i].getdPrice() &lt;&lt; endl;
        cout &lt;&lt; a[i].getdPrepareTime() &lt;&lt; endl;
        cout &lt;&lt; "INGRIDIENTS" &lt;&lt; endl;
        for (list &lt;string&gt;::iterator k = a[i].getsIngridients().begin(); k != a[i].getsIngridients().end(); k++) {
            cout &lt;&lt; *k &lt;&lt; endl;
        }
    }
    return 0;
    

    }



  • This function

    DISHDLL_API list<string> getsIngridients();
    

    Every time he returns a temporary copy of the list. Therefore, iterators received each time for these temporary facilities are incompatible because they belong to different lists.

    Change the designation of the function as follows

    DISHDLL_API list<string> & getsIngridients();
                            ^^^
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2