无法访问公共功能? 没有指定成员?

Cant access public function? no member named?

本文关键字:成员 功能 访问      更新时间:2023-10-16

我正在尝试访问该函数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()) }
}

KidsQueue()中,你的a参数是std::queue保存类型为Family的元素的实例。a本身不是Family,所以你不能对它调用Family方法。 您需要访问队列中的各个Family对象才能对它们调用Family方法,例如:

while (!a.empty()) 
{
if (a.front().getnoOfElders()) // <-- front() accesses the 1st Family object in the queue
{
...
}
a.pop();
}