急!!!友元函数重载运算符<<在类外不能使用问题。求解决、求帮忙
//=======Student.h文件========
#ifndef STUDENT_H
#define STUDENT_H
#include<string>
#include<fstream>
#include<iostream>
using std::cin;
using std::cout;
using std::ostream;
using std::istream;
using std::endl;
using std::string;
enum Sex{Girl,Boy};
const char *s[]={"Girl","Boy"};
class CStudent
{
private:
char Name[20];
Sex sex;
char Id_num[20];
float MathScores;
float EnglishScores;
public:
CStudent()
{
strcpy(Name,"");
sex=Boy; //默认为男
strcpy(Id_num,"");
MathScores=-1;
EnglishScores=-1;
}
~CStudent(){}
friend istream &operator >> (istream &in,CStudent &stu);
friend ostream &operator << (ostream &out,CStudent &stu);
};
istream &operator >> (istream &in,CStudent &stu)
{
cout<<"姓名:";in.get(stu.Name,20);
cout<<"性别:";
int s;
cin>>s;
stu.sex=(Sex)s;
cout<<"学号:";in>>stu.Id_num;
cout<<"数学成绩:";in>>stu.MathScores;
cout<<"英语成绩:";in>>stu.EnglishScores;
return in;
}
ostream &operator << (ostream &out,CStudent &stu)
{
out<<stu.Name<<" "<<s[stu.sex]<<" "<<stu.Id_num<<" "<<
stu.MathScores<<" "<<stu.EnglishScores<<endl;
return out;
}
#endif
//===========main.cpp文件==============
#include"Student.h"
#include<fstream>
#include<iostream>
using namespace std;
#define FILE "studentinfo.dat"
void Write(const CStudent &stu)
{
ofstream fout(FILE,ios::out|ios::app|ios::binary);
if(fout.fail())
{
cout<<"文件打开失败!"<<endl;
exit(1);
}
fout.write((char *)&stu,sizeof stu);
fout.close();
}
void Read(const CStudent &stu)
{
ifstream fin(FILE,ios::in|ios::binary);
if(fin.fail())
{
cout<<"文件打开失败!"<<endl;
exit(1);
}
while(fin.read((char *)&stu,sizeof stu))
cout<<stu<<endl; //这里出错,为啥头文件中重载<<不能在这里使用????
fin.close();
}
int main()
{
CStudent stu;
//cin>>stu;
//cout<<stu;
Write(stu);
Read(stu);
return 0;
}
//编译错误为:d:\vc++6.0\myprojects\查找排序\main.cpp(28) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'const class CStudent' (or there is no acceptable conversion)
执行 cl.exe 时出错.