无法访问公共功能?没有命名的成员?

问题描述 投票:0回答:1

我正在尝试访问函数getnoOfkids(),但是即使我不能访问它,为什么?我只能访问常规队列操作,例如sizeemplace等。

#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;

    queue <Family> newA,newB; queue <Family> result;
    while(!a.empty()) 
    { if(a.getnoOfElders()) }
}
c++
1个回答
1
投票

KidsQueue()中,您的a参数是std::queue个拥有类型std::queue的元素的实例。 Family本身不是a,因此无法在其上调用Family方法。您需要访问队列内的各个Family对象,以对其上的Family方法进行调用,例如:

Family
© www.soinside.com 2019 - 2024. All rights reserved.