
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;
} 展开
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;
} 展开
1个回答
展开全部
ostream &operator<<(ostream& os, People &item);
ostream& os,少个&
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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询