为什么我重载过<<之后,用cout <<*this的形式会报错,如“’operator <<’ is ambiguous”
源代码如下
#include <iostream>
#include <string>
#include <fstream>
#define N_name 20
using namespace std;
int countID; //全局变量,统计学生的编号
class CStudent
{
public:
int ID; //编号
char name[N_name]; //姓名
char gender; //性别
int nianLing; //年龄
float shuXueScore; //数学成绩
float computerScore; //计算机成绩
float englishScore; //外语成绩
void luRu(fstream &ioFile); //从键盘输入一个学生的有关信息,并将它们存入到数据文件中(按编号来确定写出位置)
void searchByID(fstream &ioFile); //按编号对学生信息进行检索并将检索结果显示在屏幕上
void searchByName(fstream &ioFile); //按姓名对学生信息进行检索并将检索结果显示在屏幕上
void sumAndAverageScore(fstream &ioFile); //计算某编号学生的总成绩与平均成绩
void displayByCondition(fstream &ioFile); //列出所有总成绩超过270分的男同学的有关信息
void displayAll(fstream &ioFile); //列出文件当前存储的所有信息
friend ostream& operator <<(ostream&,CStudent&); //运算符“<<”重载为友元函数
};
ostream& operator <<(ostream& output,CStudent& cs) //定义运算符“<<”重载函数
{
output <<"编号:"<<cs.ID<<" 姓名:";
output <<cs.name;
output <<" 性别:";
if(cs.gender==’m') output <<"男";
else output <<"女";
output <<" 年龄:"<<cs.nianLing<<"岁 数学成绩:"<<cs.shuXueScore
<<" 计算机成绩:"<<cs.computerScore<<" 外语成绩:"<<cs.englishScore<<endl;
return output;
}
void CStudent::luRu(fstream &ioFile) //从键盘输入一个学生的有关信息,并将它们存入到数据文件中(按编号来确定写出位置)
{
cout <<"**************************************************************************"<<endl;
ioFile.seekg(0,ios::end); //每次录入之前都要文件指针移到文件末尾
char choice;
ID=++countID; //对全局变量countID的操作
cout <<"姓名:";
int i;
for(i=0;i<N_name;i++) name[i]=”; //每次录入之前要对name[]数组初始化
fflush(stdin); //清空输入缓冲区
i=0;
while((name[i++]=getchar())!=’\n’);
name[i-1]=”; //去掉刚读进的’\n’
cout <<"性别(m-男性,f-女性):";
while(true)
{
cin >>choice;
if(choice==’m’ || choice==’f')
{
gender=choice;
break;
}
else
cout <<"输入有误,请重新输入:";
}
cout <<"年龄(整数):"; cin >>nianLing;
cout <<"数学成绩:"; cin >>shuXueScore;
cout <<"计算机成绩:"; cin >>computerScore;
cout <<"外语成绩:"; cin >>englishScore;
ioFile.seekg(0,ios::end); //每次添加新的数据都要把文件指针移到文件末尾
ioFile.write((char *)this,sizeof(CStudent));
cout <<"\n录入成功"<<endl;
cout <<"**************************************************************************"<<endl;
}
void CStudent::searchByID(fstream &ioFile) //按编号对学生信息进行检索并将检索结果显示在屏幕上
{
cout <<"**************************************************************************"<<endl;
cout <<"请输入要检索的学生的编号:";
int id;
while(true)
{
cin >>id;
if(id<1 || id>countID)
cout <<"没有此编号的学生,请重新输入:";
else break;
}
ioFile.seekg(0,ios::beg); //每次都从文件开头查起
ioFile.seekg((id-1)*sizeof(CStudent),ios::cur);
ioFile.read((char *)this,sizeof(CStudent));
cout <<"编号为:"<<id<<" 的学生信息为:\n";
cout <<*this;
cout <<"**************************************************************************"<<endl;
}
void CStudent::searchByName(fstream &ioFile) //按姓名对学生信息进行检索并将检索结果显示在屏幕上
{
cout <<"**************************************************************************"<<endl;
char tempName[N_name]; //存放要搜索的学生的姓名
int i;
for(i=0;i<N_name;i++) tempName[i]=”; //对tempName[]数组初始化
cout <<"请输入要查找的学生的姓名:";
fflush(stdin); //清空输入缓冲区
i=0;
while((tempName[i++]=getchar())!=’\n’);
tempName[i-1]=”; //去掉刚读进的’\n’
ioFile.seekg(0,ios::beg); //每次都从文件开头查起
for(i=0;i<countID;i++)
{
ioFile.read((char *)this,sizeof(CStudent)); //从文件读出一块数据
if(strcmp(name,tempName)==0) //如果找到则跳出,否则继续读
break;
}
if(i==countID)
cout <<"不存在此学生的信息!"<<endl;
else
{
cout <<"姓名为:"<<tempName<<" 的学生信息为:\n";
cout <<*this;
}
cout <<"**************************************************************************"<<endl;
}
void CStudent::sumAndAverageScore(fstream &ioFile) //计算某编号学生的总成绩与平均成绩
{
cout <<"**************************************************************************"<<endl;
cout <<"请输入的学生的编号:";
int id;
while(true)
{
cin >>id;
if(id<1 || id>countID)
cout <<"没有此编号的学生,请重新输入:";
else break;
}
ioFile.seekg(0,ios::beg); //每次都从文件开头查起
ioFile.seekg((id-1)*sizeof(CStudent),ios::cur);
ioFile.read((char *)this,sizeof(CStudent));
int total,average;
total=computerScore+englishScore+shuXueScore;
average=total/3;
cout <<"编号为:"<<ID<<" 的学生,姓名:"<<name<<" 总成绩="<<total <<" 平均成绩="<<average <<endl;
cout <<"**************************************************************************"<<endl;
}
void CStudent::displayByCondition(fstream &ioFile) //列出所有总成绩超过270分的男同学的有关信息
{
cout <<"**************************************************************************"<<endl;
bool flag=false; //为false说明没有符合要求的学生信息
int total;
ioFile.seekg(0,ios::beg); //每次都从文件开头查起
cout <<"总成绩超过270分的男同学的有关信息为:"<<endl;
for(int i=0;i<countID;i++)
{
ioFile.read((char *)this,sizeof(CStudent));
total=computerScore+englishScore+shuXueScore;
if(total>270 && gender==’m')
{
flag=true;
cout <<*this; //利用重载的“<<”输出this所指向的对象
}
}
if(flag==false) cout <<"不存在总成绩超过270分的男同学"<<endl;
cout <<"**************************************************************************"<<endl;
}
void CStudent::displayAll(fstream &ioFile) //列出文件当前存储的所有信息
{
cout <<"**************************************************************************"<<endl;
int k;
ioFile.seekg(0,ios::beg);
cout <<"\n文件当前信息如下:"<<endl;
for(int i=0;i<countID;i++)
{
k=0;
ioFile.read((char *)this,sizeof(CStudent));
cout <<*this; //利用重载的“<<”输出当前对象
}
cout <<endl
<<"**************************************************************************"<<endl;
}
void run(fstream &ioFile) //该学籍与成绩管理软件的入口,负责管理各个CStudent类
{
int choice;
CStudent student; //临时对象,对student操作,再读写到文件
while(true)
{
cout <<"\n1-添加新学生的有关信息"<<endl
<<"2-按编号检索学生信息"<<endl
<<"3-按姓名检索学生信息"<<endl
<<"4-计算某编号学生的总成绩与平均成绩"<<endl
<<"5-列出所有总成绩超过270分的男同学的有关信息"<<endl
<<"6-列出文件当前存储的所有信息"<<endl
<<"其他-退出程序"<<endl
<<"请选择:";
cin >>choice;
switch(choice)
{
case 1: student.luRu(ioFile); //添加新的学生信息
break;
case 2: student.searchByID(ioFile); //按编号查询
break;
case 3: student.searchByName(ioFile); //按姓名查询
break;
case 4: student.sumAndAverageScore(ioFile); //计算某学生总分及平均成绩
break;
case 5: student.displayByCondition(ioFile); //列出所有总成绩超过270分的男同学的有关信息
break;
case 6: student.displayAll(ioFile);
break;
default : return;
}
}
}
int main()
{
cout <<"*****************************************************************"<<endl
<<" 学籍与成绩管理软件 "<<endl
<<"*****************************************************************"<<endl;
fstream ioFile;
string location;
cout <<"请输入存储文件的绝对路径:";
cin >>location;
ioFile.open(location.c_str(),ios::in | ios::out | ios::binary);
if(!ioFile)
{
cout <<"没能打开指定文件! 程序退出!"<<endl;
exit(0);
}
else
cout <<"文件打开成功^_^ \n\n";
//每次启动该管理软件,打开文件时,都要统计文件中已经存了多少个学生的信息,否则,再次打开文件时,countID为0,就出错了
int count=0;
char temp;
ioFile.read((char *)&temp,1);
if(!ioFile.eof())
{
ioFile.seekg(-1,ios::cur);
while(!ioFile.eof())
{
ioFile.seekg(-1,ios::cur);
ioFile.seekg(sizeof(CStudent),ios::cur);
ioFile.read((char *)&temp,1);
++count;
}
}
countID=count; //读出当前文件中有几个学生的信息
ioFile.clear(0); //清楚错误标志位,否则文件指针会失效
run(ioFile); //启动学籍与成绩管理软件
ioFile.close();
cout <<"\n退出程序。"<<endl;
return 0;
}

你把友元函数定义也放在class 里面试试,
可能你把函数体放在外面的话,主义不明,当调用cout<<*this的时候,系统不明白C/C++ code
class CStudent
{
public:
int ID; //编号
char name[N_name]; //姓名
char gender; //性别
int nianLing; //年龄
float shuXueScore; //数学成绩
float computerScore; //计算机成绩
float englishScore; //外语成绩
void luRu(fstream &ioFile); //从键盘输入一个学生的有关信息,并将它们存入到数据文件中(按编号来确定写出位置)
void searchByID(fstream &ioFile); //按编号对学生信息进行检索并将检索结果显示在屏幕上
void searchByName(fstream &ioFile); //按姓名对学生信息进行检索并将检索结果显示在屏幕上
void sumAndAverageScore(fstream &ioFile); //计算某编号学生的总成绩与平均成绩
void displayByCondition(fstream &ioFile); //列出所有总成绩超过270分的男同学的有关信息
void displayAll(fstream &ioFile); //列出文件当前存储的所有信息
friend ostream& operator < <(ostream&,CStudent&)
{
output < <"编号:" < <cs.ID < <" 姓名:";
output < <cs.name;
output < <" 性别:";
if(cs.gender==’m') output < <"男";
else output < <"女";
output < <" 年龄:" < <cs.nianLing < <"岁 数学成绩:" < <cs.shuXueScore
< <" 计算机成绩:" < <cs.computerScore < <" 外语成绩:" < <cs.englishScore < <endl;
return output;
} ;
语义不明,打错。C/C++ code
class CStudent
{
public:
int ID; //编号
char name[N_name]; //姓名
char gender; //性别
int nianLing; //年龄
float shuXueScore; //数学成绩
float computerScore; //计算机成绩
float englishScore; //外语成绩
void luRu(fstream &ioFile); //从键盘输入一个学生的有关信息,并将它们存入到数据文件中(按编号来确定写出位置)
void searchByID(fstream &ioFile); //按编号对学生信息进行检索并将检索结果显示在屏幕上
void searchByName(fstream &ioFile); //按姓名对学生信息进行检索并将检索结果显示在屏幕上
void sumAndAverageScore(fstream &ioFile); //计算某编号学生的总成绩与平均成绩
void displayByCondition(fstream &ioFile); //列出所有总成绩超过270分的男同学的有关信息
void displayAll(fstream &ioFile); //列出文件当前存储的所有信息
friend ostream& operator < <(ostream&,CStudent&)
{
output < <"编号:" < <cs.ID < <" 姓名:";
output < <cs.name;
output < <" 性别:";
if(cs.gender==’m') output < <"男";
else output < <"女";
output < <" 年龄:" < <cs.nianLing < <"岁 数学成绩:" < <cs.shuXueScore
< <" 计算机成绩:" < <cs.computerScore < <" 外语成绩:" < <cs.englishScore < <endl;
return output;
}//少了个括号,不好意思
} ;
语义不明,打错。 C/C++ codeclass CStudent { public: int ID; //编号 char name[N_name]; //姓名 char gender; //性别 int nianLing; //年龄 float shuXueScore; //数学成绩 float computerScore; //计算机成绩 float englishScore; //外语成绩 void luRu(fstream &ioFile); //从键盘输入一个学生的有关信息,并将它们存入到数据文件中(按编号来确定写出位置) void s…ambiguous
upup
你把#include<iostream>和using namespace std;改成#include"iostream.h"
你把所有 cout<<*this的*去掉就正确了。
你把所有 cout<<*this的*去掉就正确了。