是不是VC6.0下不识别友元啊
今天写一个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;
}