无法访问公共功能? 没有指定成员?
Cant access public function? no member named?
我正在尝试访问该函数getnoOfkids()
但即使它公开我也不能,为什么?我只能访问正常的队列操作,例如size
、emplace
等。
#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();
}
相关文章:
- 类模板的成员功能的定义在单独的TU中完全专业化
- 升压信号2将插槽传递到成员功能以断开连接
- 多次实现成员功能
- 私有成员数据不可用于公共成员功能
- 如何避免大多数成员功能相同的代码重复
- C 成员功能
- =删除用户定义的成员功能,除了构造函数,分配运算符C 11
- C 朋友课程和朋友成员功能
- C 函数调用包装器包含类成员功能作为模板参数
- 创建类成员功能
- 成员功能C 的不同实现
- 返回类型不可知模板类成员功能
- C 11螺纹 - 非静态成员功能的使用无效 - 工作GCC 5.1.0损坏的GCC 7.3.1
- 超载类成员功能标记为const
- C 功能指针和成员功能指针
- 将非静态成员函数C 的使用无效,而将成员功能作为函数变量
- 我可以使用Priority_queue的基础容器的成员功能
- 如何子类可运行任何成员功能
- 共享类时如何隐藏成员功能文件
- 从C文件访问C 类成员功能