C++ 友元和重载问题,急!

zcaihyl 2009-05-22 01:01:41
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
string name;
int num,src;
student(string na="xx",int n=0,int s=0):name(na),num(n),src(s){}
friend istream & operator >>(istream &,student &);
friend ostream & operator <<(ostream &,student &);
};
istream& operator >>(istream& a1,student& a2)
{
cout<<"请输入姓名"<<endl;
a1>>a2.name;
cout<<"请输入学好"<<endl;
a1>>a2.num;
cout<<"请输入分数"<<endl;
a1>>a2.src;
cout<<endl;
return a1;
}
ostream & operator <<(ostream &a1,student &a2)
{
a1<<"结果如下"<<endl<<a2.name<<endl<<a2.num<<endl<<a2.src<<endl;
return a1;
}
int main()
{
student x,y;
cin>>x;
cout<<x;
cin>>y;
cout<<y;
return 0;
}

请问为什么有四个错误说>> 和<< 混乱;还有请问友元函数能访问类的私有成员吗?
...全文
168 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
driverSir 2011-01-27
  • 打赏
  • 举报
回复
言法错误,一点调度
zcaihyl 2009-05-22
  • 打赏
  • 举报
回复
谢谢啊,但是,我试过了,好像友元不能访问私有成员,不知道是不是编译器的问题。
ytzxj 2009-05-22
  • 打赏
  • 举报
回复
ostream & operator < <(ostream &a1,student &a2)
{
a1 < <"结果如下" < <endl < <a2.name < <endl < <a2.num < <endl < <a2.src < <endl;
return a1;
}
错了 l写成1了!!
zhangzhongke007 2009-05-22
  • 打赏
  • 举报
回复
貌似3楼的说的很对!
Sou2012 2009-05-22
  • 打赏
  • 举报
回复
以上结果是VC6下编译运行的
Sou2012 2009-05-22
  • 打赏
  • 举报
回复
测试通过



#ifndef _TTT_H_
#define _TTT_H_

#include <iostream>
#include <string>

class student
{
public:
std::string name;
int num, score;
student(std::string na = "xx", int n = 0, int s = 0) : name(na), num(n), score(s) {}
friend std::istream &operator>>(std::istream &, student &);
friend std::ostream &operator<<(std::ostream &, student &);
};

#endif



#include <iostream> 
#include <string>
#include "TTT.h"
using namespace std;

istream &operator>>(istream &a1,student &a2)
{
cout << "please enter name:" << endl;
a1 >> a2.name;
cout << "please enter your school number:" << endl;
a1 >> a2.num;
cout << "please enter your score:" << endl;
a1 >> a2.score;
cout << endl;
return a1;
}

ostream &operator<<(ostream &a1,student &a2)
{
a1 << "result as below:" << endl << a2.name << endl << a2.num << endl << a2.score << endl;
return a1;
}

int main()
{
student x, y;
cin >> x;
cout << x;
cin >> y;
cout << y;
return 0;
}

AbnormalSubmarine 2009-05-22
  • 打赏
  • 举报
回复
[Quote=引用楼主 zcaihyl 的帖子:]
#include <iostream>
#include <string>
using namespace std;
class student
{
public:
string name;
int num,src;
student(string na="xx",int n=0,int s=0):name(na),num(n),src(s){}
friend istream & operator >>(istream &,student &);
friend ostream & operator < <(ostream &,student &);
};
istream& operator >>(istream& a1,student& a2)
{
cout < <"请输入姓名" < <endl;
a1>>a2.nam…
[/Quote]

friend istream & operator >>(istream &,student &);
friend ostream & operator < <(ostream &,student &); //这个符号中间不是多了空格吗?
AbnormalSubmarine 2009-05-22
  • 打赏
  • 举报
回复
代码是好的!
可能是你的环境的问题
我在vc2005 跑你的代码一切正常
xiaoshi935 2009-05-22
  • 打赏
  • 举报
回复
如果你用vc6.0的话就是有问题的,必须实现声明,我写了一段代码,你看看先,如果还有不对的话,可以再探讨。

#include <iostream>
#include <string>
using namespace std;

class student;
istream & operator >>(istream &,student &);
ostream & operator <<(ostream &,student &);
class student {
private:
string name;
int num,src;
public:
student(string na="xx",int n=0,int s=0):name(na),num(n),src(s){}
friend istream & operator >>(istream &,student &);
friend ostream & operator <<(ostream &,student &);
};

istream& operator >>(istream& a1,student& a2) {
cout <<"请输入姓名:" <<endl;
a1>>a2.name;
cout <<"请输入学号:" <<endl;
a1>>a2.num;
cout <<"请输入分数:" <<endl;
a1>>a2.src;
cout <<endl;
return a1;
}

ostream & operator <<(ostream &a1,student &a2) {
a1 <<"结果如下:" <<endl <<a2.name <<endl <<a2.num <<endl <<a2.src <<endl;
return a1;
}

int main() {
student x,y;
cin>>x;
cout <<x;
cin>>y;
cout <<y;
return 0;
}
arong1234 2009-05-22
  • 打赏
  • 举报
回复
友元当然可以访问私有成员
至于你说的“混乱”,我估计是ambiguous?这表示模糊不清,不是混乱的意思。下次记得直接贴错误信息原文,贴自己“理解后”的文字,很可能延缓你获得答案的过程

至于ambiguous的原因:这是vc6的一个知名bug,如果友元和 std同用就会出现错误,因此立刻升级编译器(微软2005年还是2006就不再支持vs6了)

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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