error in function declaration; skipping function body

来源:百度知道 编辑:UC知道 时间:2024/05/21 09:10:53
题目如下
定义一个Person类(属性:姓名、 性别 、身份证号)并派生Teacher类(职位 、 教工号)和Student类(学号、专业)。
定义一个字符文件abc.txt,录入三个教师和四名学生的信息,重载>>和<<运算符。从文件abc.txt读入上述信息,存入学生和教师数组,按相反次序输出到屏幕显示屏和字符文件xyz.txt中.

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
class Person
{
private:
string name,sex,licence;
public:
Person(string name1,string sex1, string licence1) {name=name1;sex=sex1;licence=licence1;}
virtual void operator>>()
{
cout<<"name:";cin>>name;cout<<" sex:";cin>>sex;cout<<" licence:";cin>>licence;cout<<endl;
}
virtual void operator <<()
{
cout<<"name:"<<name<<" sex:"<<sex<<" licence:"<<licence<<endl;
}
};

class Teacher:public Person
{

重载输入输出流函数需要写成FRIEND函数,所以不是成员函数,不能写在类里面,需要写成全局的,格式如下:
ostream &operaor<<(oostream &os,const ClassName &)
{
...............
return os;
}
你希望在流重载函数里面既要要读入又要输出,这我没试成功,你最好就用一般的虚成员函数就行了,别重载流函数
纯个人观点

纯个人观点

错误提示error in function declaration; skipping function body
指函数声明错误,跳过了函数体,

好长时间没看c++了,都忘了,呵呵,不好意思哦