c++文件和重载输出符问题
#include<iostream>#include<string>#include<fstream>#include<stdlib.h>usingnamespacest...
#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
using namespace std;
class Cstudents
{
public:
int num;
char name[30];
char gender;
int age;
float match;
float pc;
float en;
float sum;
float average;
Cstudents()
{
num=0;
strcpy(name," ");
gender=' ';
age=0;
match=0;
pc=0;
en=0;
sum=0;
average=0;
}
friend istream& operator>>(istream&,Cstudents&);
friend ostream& operator>>(ostream&,Cstudents&);
};
istream& operator>>(istream& is,Cstudents& c)
{
is>>c.name>>c.gender>>c.age>>c.match>>c.pc>>c.en;
return is;
}
ostream& operator>>(ostream& os,Cstudents& c)
{
os<<c.num<<c.name<<c.gender<<c.age<<c.match<<c.pc<<c.en;
return os;
}
void main()
{
Cstudents stu[20];
ofstream fout;
ifstream fin;
int i=1,n,max=1;
fout.open("students.txt",ios::out|ios::binary);
if(!fout)
{
cout<<"错误";
exit(1);
}
cout<<"输入学号:(0号为结束输入)";
while(cin>>n&&0!=n)
{
stu[n].num=n;
cout<<"输入名字 性别 年龄 数学 计算机 外语";
cin>>stu[n];
if(n>max)max=n;
}
while(i<max+1)
{
fout.write((char*)&stu[i],sizeof(Cstudents));
i++;
}
fout.close();
fin.open("students.txt",ios::in|ios::binary);
if(!fin)
{
cout<<"错误";
exit(1);
}
cout<<"num\t"<<"name\t"<<"sex"<<"age\t"<<"match\t"<<"pc\t"<<"en\t";
i=1;
while(fin&&i<max)
{
fin.read((char*)&stu[i],sizeof(Cstudents));
cout<<stu[i];
i++;
}
fin.close();
}
1>------ 已启动生成: 项目: lzz, 配置: Debug Win32 ------
1>正在编译...
1>Cstudents.cpp
1>d:\课件\程序\lzz\lzz\cstudents.cpp(21) : warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files\microsoft visual studio 9.0\vc\include\string.h(74) : 参见“strcpy”的声明错误提示:
1>d:\课件\程序\lzz\lzz\cstudents.cpp(81) : error C2679: 二进制“<<”: 没有找到接受“Cstudents”类型的右操作数的运算符(或没有可接受的转换) 展开
#include<string>
#include<fstream>
#include<stdlib.h>
using namespace std;
class Cstudents
{
public:
int num;
char name[30];
char gender;
int age;
float match;
float pc;
float en;
float sum;
float average;
Cstudents()
{
num=0;
strcpy(name," ");
gender=' ';
age=0;
match=0;
pc=0;
en=0;
sum=0;
average=0;
}
friend istream& operator>>(istream&,Cstudents&);
friend ostream& operator>>(ostream&,Cstudents&);
};
istream& operator>>(istream& is,Cstudents& c)
{
is>>c.name>>c.gender>>c.age>>c.match>>c.pc>>c.en;
return is;
}
ostream& operator>>(ostream& os,Cstudents& c)
{
os<<c.num<<c.name<<c.gender<<c.age<<c.match<<c.pc<<c.en;
return os;
}
void main()
{
Cstudents stu[20];
ofstream fout;
ifstream fin;
int i=1,n,max=1;
fout.open("students.txt",ios::out|ios::binary);
if(!fout)
{
cout<<"错误";
exit(1);
}
cout<<"输入学号:(0号为结束输入)";
while(cin>>n&&0!=n)
{
stu[n].num=n;
cout<<"输入名字 性别 年龄 数学 计算机 外语";
cin>>stu[n];
if(n>max)max=n;
}
while(i<max+1)
{
fout.write((char*)&stu[i],sizeof(Cstudents));
i++;
}
fout.close();
fin.open("students.txt",ios::in|ios::binary);
if(!fin)
{
cout<<"错误";
exit(1);
}
cout<<"num\t"<<"name\t"<<"sex"<<"age\t"<<"match\t"<<"pc\t"<<"en\t";
i=1;
while(fin&&i<max)
{
fin.read((char*)&stu[i],sizeof(Cstudents));
cout<<stu[i];
i++;
}
fin.close();
}
1>------ 已启动生成: 项目: lzz, 配置: Debug Win32 ------
1>正在编译...
1>Cstudents.cpp
1>d:\课件\程序\lzz\lzz\cstudents.cpp(21) : warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files\microsoft visual studio 9.0\vc\include\string.h(74) : 参见“strcpy”的声明错误提示:
1>d:\课件\程序\lzz\lzz\cstudents.cpp(81) : error C2679: 二进制“<<”: 没有找到接受“Cstudents”类型的右操作数的运算符(或没有可接受的转换) 展开
2个回答
展开全部
流运算符的重载要以友元函数的形式,并且声明和定义要放在一起。
改成这样,孙庆就可以了
#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
using namespace std;
class Cstudents
{
public:
int num;
char name[30];
char gender;
int age;
float match;
float pc;
float en;
float sum;
float average;
Cstudents()
{
num=0;
strcpy(name," ");
gender=' ';
age=0;
match=0;
pc=0;
en=0;
sum=0;
average=0;
}
friend istream& operator>>(istream& is,Cstudents& c)
{
is>>c.name>>c.gender>>c.age>>c.match>>c.pc>>c.en;
return is;
}
friend ostream& operator<<(ostream& os,Cstudents& c)// <<符号也输错了
{
os<<c.num<<c.name<<c.gender<<c.age<<c.match<<c.pc<<c.en;
return os;
}
};
void main()
{
Cstudents stu[20];
ofstream fout;
ifstream fin;
int i=1,n,max=1;
fout.open("橘手students.txt",ios::out|ios::binary);
if(!fout)
{
cout<<"错误";
exit(1);
}
cout<<"输入学号:(0号为结束输入)";
while(cin>>n&&0!=n)
{
stu[n].num=n;
cout<<"输入圆凯嫌名字 性别 年龄 数学 计算机 外语";
cin>>stu[n];
if(n>max)max=n;
}
while(i<max+1)
{
fout.write((char*)&stu[i],sizeof(Cstudents));
i++;
}
fout.close();
fin.open("students.txt",ios::in|ios::binary);
if(!fin)
{
cout<<"错误";
exit(1);
}
cout<<"num\t"<<"name\t"<<"sex"<<"age\t"<<"match\t"<<"pc\t"<<"en\t";
i=1;
while(fin&&i<max)
{
fin.read((char*)&stu[i],sizeof(Cstudents));
cout<<stu[i];
i++;
}
fin.close();
}
改成这样,孙庆就可以了
#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
using namespace std;
class Cstudents
{
public:
int num;
char name[30];
char gender;
int age;
float match;
float pc;
float en;
float sum;
float average;
Cstudents()
{
num=0;
strcpy(name," ");
gender=' ';
age=0;
match=0;
pc=0;
en=0;
sum=0;
average=0;
}
friend istream& operator>>(istream& is,Cstudents& c)
{
is>>c.name>>c.gender>>c.age>>c.match>>c.pc>>c.en;
return is;
}
friend ostream& operator<<(ostream& os,Cstudents& c)// <<符号也输错了
{
os<<c.num<<c.name<<c.gender<<c.age<<c.match<<c.pc<<c.en;
return os;
}
};
void main()
{
Cstudents stu[20];
ofstream fout;
ifstream fin;
int i=1,n,max=1;
fout.open("橘手students.txt",ios::out|ios::binary);
if(!fout)
{
cout<<"错误";
exit(1);
}
cout<<"输入学号:(0号为结束输入)";
while(cin>>n&&0!=n)
{
stu[n].num=n;
cout<<"输入圆凯嫌名字 性别 年龄 数学 计算机 外语";
cin>>stu[n];
if(n>max)max=n;
}
while(i<max+1)
{
fout.write((char*)&stu[i],sizeof(Cstudents));
i++;
}
fout.close();
fin.open("students.txt",ios::in|ios::binary);
if(!fin)
{
cout<<"错误";
exit(1);
}
cout<<"num\t"<<"name\t"<<"sex"<<"age\t"<<"match\t"<<"pc\t"<<"en\t";
i=1;
while(fin&&i<max)
{
fin.read((char*)&stu[i],sizeof(Cstudents));
cout<<stu[i];
i++;
}
fin.close();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询