Возвращать новую очередь из двух объектов очереди? - PullRequest
0 голосов
/ 07 октября 2019

Я пытаюсь реализовать функцию KidsQueue, которая должна переместить семьи с более чем 3 детьми в новый счетчик и вернуть его в качестве новой очереди, но счетчики остаются неизменными, а счетчик 3 содержит семью с числом детей выше3

#include <iostream>
#include <queue>
using namespace std;
class Family
{
private:
    int id, noOfElders, noOfKids;
public:
    bool operator ==(const  Family &f)
    {
        if ((this->id!= f.id) || (this->noOfElders != f.noOfElders)||(this->noOfKids != f.noOfKids))
        {
            return false;
        }
        return true;


    }
    bool operator !=(const  Family &f)
    {
        return !(*this==f); //////////////////////
    }
    Family(int ide=0, int eld=0, int kid=0) {
        noOfElders = eld;
        noOfKids = kid;
        id = ide;
    }

    Family(const Family &a) {
        noOfKids = a.noOfKids;
        noOfElders = a.noOfElders;
        id = a.id;
    }

    Family operator=(Family const &a) {
        this->id = a.id;
        this->noOfElders = a.noOfElders;
        this->noOfKids = a.noOfKids;
        return *this;

    }

    int getnoOfkids() const  {
        return noOfKids;
    }
    int getnoOfElders() const {
        return noOfElders;
    }
    int getid() const {
        return id;
    }
    void setnoOfKids(int x) {
        noOfKids = x;
    }
    void setnoOfElders(int x) {
        noOfElders = x;
    }
    void setid(int x) {
        id = x;
    }

    friend ostream & operator<<(ostream & out, const Family & a)
    {
        out << "The id of the travelers are: " << a.id << endl;
        out << "The number of elders are: " << a.noOfElders << endl;
        out << "The number of kids are: " << a.noOfKids << endl;
        return out;
    }

    friend istream &operator >> (istream &in, Family &a) {
        in >> a.id;
        in >> a.noOfElders;
        in >> a.noOfKids;

        return in;
    }


};

queue<Family> KidsQueue(queue<Family> &a, queue<Family> &b) {
    queue <Family> newA,newB;
    queue <Family> result;

    while(!a.empty())
    { if(a.front().getnoOfkids()>3) {
            result.push(a.front()); a.pop();
        }
        else {
            newA.push(a.front());
            a.pop();
        }
    }
    while(!b.empty())
    { if(b.front().getnoOfkids()>3) {
            result.push(b.front()); b.pop();
        }
        else {
            newB.push(b.front());
            b.pop();
        }
    }
    a=newA;
    b=newB;
    return result;
}


        //print Queue
        ostream &operator<<(ostream &out, const queue<Family> &q) {
            queue<Family> tmp = q;
            while (!tmp.empty()) {
                cout << tmp.front() << endl;
                tmp.pop();
            }

            return out;
        }


        int main() {
            queue <Family> counter1;
            queue <Family> counter2;
            queue <Family> counter3;

            counter1.push(Family(100, 2, 3));
            counter1.push(Family(200, 2));
            counter1.push(Family(400, 1));
            counter1.push(Family(402, 1,4));
            counter1.push(Family(789, 2));

            counter2.push(Family(300, 2, 1));
            counter2.push(Family(500, 1, 3));
            counter2.push(Family(405, 3, 2));
            counter2.push(Family(309, 1, 3));
            counter2.push(Family(567, 2));


            counter3 = KidsQueue(counter1, counter2);

            cout << "Counter 3 has: " << endl << counter3;
            cout << "Counter 1 after modification has: \n" << counter1;
            cout << "Counter 2 after modification has: " << endl << counter2;

            system("pause");
            return 0;
        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...