error C2061: syntax error : identifier 'fstream'
#ifndef _ _COMPANY_STAFF_H_ _//如果没有定义_ _COMPANY_STAFF_H_ _
#define _ _COMPANY_STAFF_H_ _//那么定义_ _COMPANY_STAFF_H_ _
//员工类
class Staff {
protected:
//数据成员
char num[8];//编号
char name[12];//姓名
int age;//年龄
public:
//构造函数,析构函数和方法
Staff() {}//构造函数
virtual ~Staff() {}//析构函数
const char *GetNum() const { return num; }//返回编号
virtual void Input() = 0;//输入函数
virtual void Show() const = 0;//显示函数
virtual void Read(fstream &f) const = 0;//读文件
virtual void Write(fstream &f) const = 0;//写文件
};
/***************************************************************************************/
//工程师类
class Engineer:virtual public Staff {
protected:
//数据成员
char major[12];//专业
char prof[12];//职称
public:
//构造函数,析构函数和方法
Engineer() {}//构造函数
virtual ~Engineer() {}//析构函数
voidInput()//输入函数
{
//输入相关信息
cout<<"编号:";
cin<<num;//输入编号
cout<<"姓名:";
cin<<name;//输入姓名
cout<<"年龄:";
cin<<age;//输入年龄
cout<<"专业:";
cin<<major;//输入专业
cout<<"职称:";
cin<<prof;//输入职称
}
void Show() const//显示数据
{
//显示相关信息
cout<<setw(8)<<num<<setw(12)<<name<<setw(5)<<age<<setw(12)<<major<<setw(12)<<prof<<endl;//setw(int n)用来控制输出间隔。
}
void Read(fstream &f) const { f.read((char*)this,sizeof(Engineeer)); }//读文件
void Write(fstream &f) const { f.write((char*)this,sizeof(Engineeer)); }//写文件
};
/***************************************************************************************/
//领导类
class Leader:virtual public Staff {
protected:
//数据成员
char job[12];//职务
char dep[12];//部门
public:
//构造函数,析构函数和方法
Leader() {}//构造函数
virtual ~Leader() {}//析构函数
void Input()//输入函数
{
//输入相关信息
cout<<"编号:";
cin<<num;//输入编号
cout<<"姓名:";
cin<<name;//输入姓名
cout<<"年龄:";
cin<<age;//输入年龄
cout<<"职务:";
cin<<job;//输入职务
cout<<"部门:";
cin<<dep;//输入部门
}
void Show() const//显示数据
{
//显示相关信息
cout<<setw(8)<<num<<setw(12)<<name<<setw(5)<<age<<setw(12)<<job<<setw(12)<<dep<<endl;//setw(int n)用来控制输出间隔。
}
void Read(fstream &f) const { f.read((char*)this,sizeof(Leader)); }//读文件
void Write(fstream &f) const { f.write((char*)this,sizeof(Leader)); }//写文件
};
/***************************************************************************************/
//主任工程师类
class Chairman:public Engineer,public Leader {
public:
//构造函数,析构函数和方法
Chairman() {}//构造函数
virtual ~Chairman() {}//析构函数
void Input()//输入函数
{
//输入相关信息
cout<<"编号:";
cin<<num;//输入编号
cout<<"姓名:";
cin<<name;//输入姓名
cout<<"年龄:";
cin<<age;//输入年龄
cout<<"专业:";
cin<<major;//输入专业
cout<<"职称:";
cin<<prof;//输入职称
cout<<"职务:";
cin<<job;//输入职务
cout<<"部门:";
cin<<dep;//输入部门
}
void Show() const//显示数据
{
//显示相关信息
cout<<setw(8)<<num<<setw(12)<<name<<setw(5)<<age<<setw(12)<<major<<setw(12)<<prof<<setw(12)<<job<<setw(12)<<dep<<endl;//setw(int n)用来控制输出间隔。
}
void Read(fstream &f) const { f.read((char*)this,sizeof(Chairman)); }//读文件
void Write(fstream &f) const { f.write((char*)this,sizeof(Chairman)); }//写文件
};
/***************************************************************************************/
//索引结构
struct IndexType {
bool delTag;//删除标志
char num[8];//编号
int position;//在数据文件中相应的位置
char staffType;//人员类型,e:工程师 l:领导 c:主任工程师
};
#define NUM_OF_INCREMENT 1000//增量
/***************************************************************************************/
//员工管理类
class StaffManage {
private:
//数据成员
fstream file;//人员数据文件
IndexType *indexTable;//索引表
int maxSize;//索引表最大索引项个数
int count;//索引项个数
//辅助函数
void AddIndexTtem(const IndexType &e);//在索引表中增加索引项
void AddData();//增加数据
void UpdateData();//更新数据
void SearchData();//查询数据
void DeleteData();//删除数据,只作删除标志
void Pack();//在物理上删除有删除标记的记录
public:
//构造函数,析构函数和方法
StaffManege() {};//无参构造函数
virtual ~StaffMnage() {};//析构函数
void Run();//处理电话号码簿
};
/***************************************************************************************/
//员工管理类的实现
StaffManage::StaffManage()//无参构造函数
{
ifstream indexFile("staff.idx",ios::binary);//建立输入索引文件
if (!indexFile.fail())
{
//打开文件成功
indexFile.seekg(0,ios::end);//定位
count = indexFile.tellg()/sizeof(IndexType);//索引项个数
maxSize = count + NUM_OF_INCREMENT;//索引表最大索引项个数
indexTable = new IndexType[maxSize];//为索引表分配存储空间
indexFile.seekg(0,ios::beg);//定位
int i = 0;//临时变量
indexFile.read((char *) &indewxTable[i++],sizeof(IndexType));//读记录
while(!indexFile.eof())
{//文件未结束
indexFile.read((char *) &indewxTable[i++],sizeof(IndexType));//读记录
}
indexFile.close();//文件关闭
}
else
{
//文件打开失败
count = 0//索引项个数
maxSize = count + NUM_OF_INCREMENT;//索引表最大索引项个数
indexTable = new IndexType[maxSize];//为索引表分配存储空间
}
ifstream iFile("staff.dat");//建立输入文件
if(iFile.fail())
{
//打开文件失败,表示不存在文件
ofstream oFile("Staff.dat");//建立输出文件
if(oFile.fail())throw("打开文件失败!");//抛出异常
oFile.close();//关闭文件
}
else
{//存在文件
iFile.close();
}
file.open("staff.dat",ios::in|ios::out|ios::binary);//以读写方式打开文件
if(file.fail()) throw ("打开文件失败!");//抛出异常
}
/***************************************************************************************/
StaffManage::~StaffManage()//析构函数
{
ofstream indexFile("staff.idx",ios::binary);//建立输出索引文件
for (int i = 0;i<count;i++)
{//写索引表
indexFile.write((char *) &indexTable[i],sizeof(IndexType));//写文件
}
indexFile.close();//关闭文件
file.close();//关闭文件
}
/***************************************************************************************/
void StaffManage::AddIndexItem(const IndexType &e)//在索引表中增加索引项
{
if(count>=maxsize)
{//索引表溢出
maxSize = maxSize + NUM_OF_INCREMENT;//增加索引表容量
IndexType *tmpIndexTable = new IndexType[maxSize];//临时索引表
for (int i = 0;i<count;i++)
tmpIndexTable[i] = indexTable[i];//复制索引表
delete []indexTable;//释放索引表存储空间
indexTable = tmpIndexTable;//指向临时索引表
}
indexTable[count++]=e;//加入到索引表中
}
/***************************************************************************************/
void StaffManage::AddData()//增加数据
{
Staff *pStaff;//人员
IndexType item;//索引表
item.delTag = false;//删除标志
file.seekg(0,ios::end);//定位
do
{
do
{
cout<<"人员类型(e:工程师,l:领导,c:主任工程师):";
cin>>item.staffType;//输入人员类型
item,StaffType = tolower(item.staffType);//转换为小写字母
} while(item.staffType!='e'&&item.staffType!='l'&&item.staffType!='c');
if (item.staffType=='e') pStaff = new Engineer;//分配存储空间
else if (item.staffType=='l') pStaff = new Leader;//分配存储空间
else pStaff = new Chairman;//分配存储空间
pStaff->Input();//输入数据
strcpy(item.num,pStaff->GetNum());//复制编号
item.position = file.tellg();//写入数据文件的位置
AddIndeaItem(item);//在索引表中增加索引项
pStaff->Write(file);//写文件
delete pStaff;//释放存储空间
cout<<"继续添加吗?";
}
while (UserSayYes());//肯定回答时循环
}
/***************************************************************************************/
void StaffManege::UpdateData()//更新数据
{
Staff *pStaff;//人员
IndexType item;//索引项
item.delTag = false;//删除标志
char num [12];//编号
cout<<"人员编号:";
cin>>num;//输入人员编号
int pos;//临时变量
for (pos = 0;pos<count;pos++)
{//查找满足条件的索引项
if (strcmp(num,indexTable[pos].num)==0 && !indexTable[pos].delTag)//编号相同or未被删除
break;
}
if(pos<count)
{//查询成功
file.seekg(indexTable[pos].position,ios::beg);//定位
if (indexTable[pos].staffType= ='e') pStaff = new Engineer;//分配存储空间
else if (indexTable[pos].staffType=='l') pStaff = new Leader;//分配存储空间
else pStaff = new Chairman;//分配存储空间
pStaff->Read(file);//读文件
pStaff->Show();//显示信息
do
{
cout<<"人员类型(e:工程师,l:领导,c:主任工程师):";
cin>>item.staffType;//输入人员类型
item,StaffType = tolower(item.staffType);//转换为小写字母
} while(item.staffType!='e'&&item.staffType!='l'&&item.staffType!='c');
if (item.staffType=='e') pStaff = new Engineer;//分配存储空间
else if (item.staffType=='l') pStaff = new Leader;//分配存储空间
else pStaff = new Chairman;//分配存储空间
pStaff->Input();//输入信息
if(item.staffType==indexTable[pos].staffType)
{//人员类型未变
strcpy(indexTable[pos].num,pStaff->GetNum());//复制编号
file.seekg(indexTable{pos].position,ios::beg);//定位
}
else
{//人员类型已变
indexTable[pos].delTag = true;//删除标志
strcpy(item.num,pStaff->GetNum());//复制编号
file.seekg(0,ios::end);//定位
item.position = file.tellg();//写入数据文件的位置
AddIndexItem(item);//在索引表中添加索引项
}
pStaff->Write(file);//写文件
delete pStaff;//释放存储空间
}
else
{//查询失败
cout<<"查无此人!"<<endl;
file.clear();//恢复好标志位 goodbit
}
[解决办法]
#include <fstream>
using std::fstream;
[解决办法]
#ifndef _ _COMPANY_STAFF_H_ _ //如果没有定义_ _COMPANY_STAFF_H_ _改为
#define _ _COMPANY_STAFF_H_ _ //那么定义_ _COMPANY_STAFF_H_ _
#ifndef __COMPANY_STAFF_H__ //如果没有定义__COMPANY_STAFF_H__
#define __COMPANY_STAFF_H__ //那么定义__COMPANY_STAFF_H__
[解决办法]
#include<ifstream>
using std::fstream
在程序最前面。哪里用到哪里加