是不是VC6.0下不识别友元啊

Hammer42 2012-05-14 10:51:27
今天写一个C++的程序,突然发现报错说不能访问类中的private成员,但是我已经把此函数说明程友元了啊!大家看看怎么回事,错误从注解的地方开始
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

const int MAX_NUM_OF_COURSES=30;
const int MAX_ID_LEN=10;
const int MAX_NAME_LEN=8;
class StudentScores
{
public:
StudentScores(){init=false;}
bool date_is_ok(){return init;}
private:
int scores[MAX_NUM_OF_COURSES],num_of_couress;
char id[MAX_ID_LEN],name[MAX_NAME_LEN+1];
bool init;
friend istream &operator >>(istream &in,StudentScores &x);
friend ostream &operator <<(ostream &out,StudentScores &x);
};
istream &operator >>(istream &in,StudentScores &x)
{
if(&in==&cin) cout<<"请输入学号,姓名,选课数以及各门课成绩(以学号为'E'结束):\n";
//in>>setw(11)>>x.id;
//if(in.eof()||x.id[0]=='E')
{
x.init=false;
return in;
}
in>>setw(9)>>x.name;
in>>x.num_of_couress;
if(x.num_of_couress>MAX_NUM_OF_COURSES)
{
x.init=false;
return in;
}
for(int i=0;i<x.num_of_couress;i++)
in>>x.scores[i];
x.init=true;
return in;
}

ostream &operator <<(ostream &out,StudentScores &x)
{
out<<x.id<<' '<<x.name<<' '<<x.num_of_couress;
for(int i=0;i<x.num_of_couress;i++)
out<<' '<<x.scores[i];
return out;
}
int main()
{
ofstream out_file("D:\\score.txt",ios::out);
if(!out_file)
{
cerr<<"打开文件失败!!!\n";
return -1;
}
StudentScores st;
cin>>st;
while(st.date_is_ok())
{
out_file <<st<<endl;
cin>>st;
}
out_file.close();
return 0;
}
...全文
199 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

引用 3 楼 的回复:

来迟了


忘了说了,我还是不懂为什么会出现这样,和书上说的明显不一样啊 啊,大家解释一下啊
[/Quote]
链接上不是说明了么,VC6的bug。
VS2008 VS2010 就没这个bug了
Hammer42 2012-05-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

来迟了
[/Quote]

忘了说了,我还是不懂为什么会出现这样,和书上说的明显不一样啊 啊,大家解释一下啊
W170532934 2012-05-15
  • 打赏
  • 举报
回复
来迟了
Hammer42 2012-05-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

http://www.cnblogs.com/xinjun/archive/2010/07/19/1780902.html
[/Quote]


谢谢!已解决
帅得不敢出门 2012-05-15
  • 打赏
  • 举报
回复
vc6对友元模版支持不行。
牙牙学语 2012-05-15
  • 打赏
  • 举报
回复
不要用6.0了吧,装个级别高点的呗
牙牙学语 2012-05-15
  • 打赏
  • 举报
回复
#include <iostream.h>
#include <fstream>
#include <iomanip>
//using namespace std;
我记得好像是<iostream.h>,并且using namespace std;得注释掉的,当年做的时候,书上好像是这样说的,

64,666

社区成员

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

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