C++定义重载输出运算符的问题,我不知道到怎么调用啊!总报错是引用已删除元素是怎么回事!

#include<iostream>usingnamespacestd;classPeople{friendostream&operator<<(ostreamos,Pe... #include <iostream>
using namespace std;
class People {
friend ostream &operator<<(ostream os, People &item);
public:
People(){}
People(int h, int a) : hight(h), age(a) {}
~People(){}
People(const People&);
int gethight() { return hight; }
int getage() { return age; }
private:
int hight;
int age;
};
People::People(const People &p) : hight(p.hight), age(p.age) {}
ostream& operator<<(ostream os, People &item)
{
os << "此人身高" << item.gethight() << "cm" << " " << "此人年龄" << item.getage() << endl;
return os;
}
int main(void)
{
People p1(180, 20);
cout << p1;
return 0;
}
展开
 我来答
野文菜A
2014-08-01 · TA获得超过830个赞
知道小有建树答主
回答量:645
采纳率:100%
帮助的人:562万
展开全部
ostream &operator<<(ostream& os, People &item);
ostream& os,少个&
更多追问追答
追问
好吧是我疏漏了,可是
istream &operator>>(istream &is, People &item)
{
is >> item.gethight() >> item.getage() >> endl;
return is;
}
重载输入运算符没有问题吧?为什么is >> item.gethingt()里面的>>会报错??
追答
格式没有问题,但是你不能把函数的返回结果赋值,也不能>>endl;
istream &operator>>(istream &is, People &item)
{
is >> item.hight>> item.age;
return is;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式