C++输入输出运算符重载问题

abc_student 2009-12-26 09:19:27
在类中进行了输入输出运算符的声明和定义,在主函数中怎么调用呢?
例如:
class student
{
//运算符重载
friend ostream &operator<<(ostream &os,student &s);
friend istream &operator>>(istream &is,student &s);

int number,age;
double chinese,math,english,average,sum;
char name[20],sex[10],born[10];
public:
student()
{
}

student(char n[20],char sx[10],char bor[10],int ag,int nu,double chin,double ma,double eng);


double getsum();//求总分

double getaver();//求平均分
display();

friend void main();
};

ostream &operator<<(ostream &os,student &s)
{
cout<<"姓名\t"<<"性别\t"<<"生日\t"<<"年龄\t"<<"学号\t"<<"语文\t"<<"数学\t"<<"英语\t"<<endl;
os<<s.name<<"\t"<<s.sex<<"\t"<<s.born<<"\t"<<s.age<<"\t"<<s.number<<"\t"<<s.chinese<<"\t"<<s.math<<"\t"<<s.english<<endl;
return os;
}
……………………
……………………
void main()
{
......
......
}
...全文
603 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
febbird1984 2009-12-27
  • 打赏
  • 举报
回复
ostream &operator < <(ostream &os,student &s)
{
cout < <"姓名\t" < <"性别\t" < <"生日\t" < <"年龄\t" < <"学号\t" < <"语文\t" < <"数学\t" < <"英语\t" < <endl; //cout应该是os, 不过我觉得是你手误打错的.
os < <s.name < <"\t" < <s.sex < <"\t" < <s.born < <"\t" < <s.age < <"\t" < <s.number < <"\t" < <s.chinese < <"\t" < <s.math < <"\t" < <s.english < <endl;
return os;
}

其他地方直接调用就可以了啊,如4楼所描述的那样.这里重载的cout和cin不是student类的成员函数,还是std中的cout和cin
kxalpah 2009-12-26
  • 打赏
  • 举报
回复
直接使用就可以啊,还有哪个成员函数被调用了?
abc_student 2009-12-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 stardust20 的回复:]
student a;//建议类名首字母大写
cin>>a;//这样就可以了
cout < <a;
[/Quote]
这样的话,别的成员函数也被调用了啊,我只需要调用运算符这个友元函数,那怎么办呢?
stardust20 2009-12-26
  • 打赏
  • 举报
回复
student a;//建议类名首字母大写
cin>>a;//这样就可以了
cout<<a;
_JeffreyWu 2009-12-26
  • 打赏
  • 举报
回复
学习
xu_cq2004 2009-12-26
  • 打赏
  • 举报
回复
vs.net 2005
yilaozhuang 2009-12-26
  • 打赏
  • 举报
回复
成员函数,数据成员
mstlq 2009-12-26
  • 打赏
  • 举报
回复
如果用的是vc6,换一个编译器就好了……
这个是著名的bug……

学标准c++,vc6很不好用……
tan870426 2009-12-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 abc_student 的回复:]
引用 4 楼 stardust20 的回复:
student a;//建议类名首字母大写
cin>>a;//这样就可以了
cout < <a;

这样的话,别的成员函数也被调用了啊,我只需要调用运算符这个友元函数,那怎么办呢?
[/Quote]
别的成员函数是什么意思?

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧