问题描述
- error C2248: 'Num' : cannot access private ......
-
#include
#include
#include
#include
using namespace std;
class worker{
string Num; //职工号
string Nam; //姓名
string Pen; //性别
int Age; //年龄
double Sal; //工资
public:
worker(string="#",string="#",string="#",int=0,double=0);
friend ostream & operator<<(ostream & dist,worker & iv); //重载插入运算符
friend istream & operator>>(istream & sour,worker & iv); //重载提取运算符
bool operator==(worker &);
bool operator<=(worker &);
};
worker::worker(string num,string nam,string pen,int age,double sal){
Num=num;
Nam=nam;
Pen=pen;
Age=age;
Sal=sal;;
}
ostream & operator<<(ostream & dist,worker & iv){
dist<
dist
return dist;
}
istream&operator>>(istream & sour,worker & iv){
if(sour==cin){
cout<<"请输入职工号"<
sour>>iv.Num;
cout<<"请输入姓名"<
sour>>iv.Nam;
cout<<"请输入性别"<
sour>>iv.Pen;
cout<<"请输入年龄"<
sour>>iv.Age;
cout<<"请输入工资"<
sour>>iv.Sal;
}
else sour>>iv.Num>>iv.Nam>>iv.Pen>>iv.Age>>iv.Sal;
return sour;
}
bool worker::operator==(worker & wor){return Num==wor.Num;}
bool worker::operator<=(worker & wor){return Num<=wor.Num;}
template
class Array
{
T*elements;
int Subscript;
int maxSize;
fstream datafile;
public:
Array(int=2); //为了便于检验,默认元素数暂时为2
~Array();
bool IsFull()const{return Subscript==maxSize-1;}
void renews(); //内存扩大一倍
void show(){
cout<<"已用最大下标值"<
}
void ordinsert(T&); //输入时以职工号为关键字
friend ostream&operator
};
templateArray::Array(int maxs){
maxSize=maxs;
Subscript=-1; //私有数据不允许直接赋初值,必须在构造函数中赋初值
T temp;
elements=new T[maxSize];
datafile.open("mydatafile.txt",ios::in); //如文件不存在,则打开失败
if(!datafile==0){
while(!datafile.eof()){
datafile>>temp;
if(datafile.eof()==0){ //读到无数据可读后,即读入不成功,eofbit为1
ordinsert(temp); //即使原文件未排序,退出时也安排好序的重存
}
}
datafile.close(); //必须放此处,打开成功才能关闭
}
datafile.clear(0); //前面曾经读到文件结束或文件打开失败,流无法恢复
}
templateArray::~Array(){
int i;
datafile.open("mydatafile.txt",ios::out);
for(i=0;i<=Subscript;i++)datafile<
datafile.close();
delete[ ]elements;
}
templatevoid Array::renews(){
int i;
T*temp=elements;
maxSize*=2;
elements=new T[maxSize];
for(i=0;i<=Subscript;i++) elements[i]=temp[i];
delete[ ]temp;
}
templatevoid Array::ordinsert(T & elem){ //输入时以职工号为关键词
int i;
if(IsFull())renews();
for(i=0;i<=Subscript;i++)if(elem<=elements[i])break;
if(!(elem==elements[i])){
Subscript++;
}
elements[i]=elem;
show();
}
templateostream&operator<<(ostream&dist,Array&ar){
int i;
for(i=0;i<=ar.Subscript;i++) cout<
return dist;
}
int main(){
Arraymylist;
worker temp;
char ch;
cout<<"是否输入新的职工记录?Y or N"<
cin>>ch;
while(ch=='Y'||ch=='y'){
cin.get();
cin>>temp;
mylist.ordinsert(temp);
cout<<"是否继续输入?Y or N"<
cin>>ch;
}
cout<<mylist;
return 0;
}报错:
c:usersadministratordesktopss.cpp(28) : error C2248: 'Num' : cannot access private member declared in class 'worker'
c:usersadministratordesktopss.cpp(8) : see declaration of 'Num'
c:usersadministratordesktopss.cpp(28) : error C2248: 'Nam' : cannot access private member declared in class 'worker'
c:usersadministratordesktopss.cpp(9) : see declaration of 'Nam'
c:usersadministratordesktopss.cpp(28) : error C2248: 'Pen' : cannot access private member declared in class 'worker'
c:usersadministratordesktopss.cpp(10) : see declaration of 'Pen'
c:usersadministratordesktopss.cpp(29) : error C2248: 'Age' : cannot access private member declared in class 'worker'
c:usersadministratordesktopss.cpp(11) : see declaration of 'Age'
c:usersadministratordesktopss.cpp(29) : error C2248: 'Sal' : cannot access private member declared in class 'worker'
c:usersadministratordesktopss.cpp(12) : see declaration of 'Sal'
c:usersadministratordesktopss.cpp(35) : error C2248: 'Num' : cannot access private member declared in class 'worker'
c:usersadministratordesktopss.cpp(8) : see declaration of 'Num'
c:usersadministratordesktopss.cpp(37) : error C2248: 'Nam' : cannot access private member declared in class 'worker'
c:usersadministratordesktopss.cpp(9) : see declaration of 'Nam'
c:usersadministratordesktopss.cpp(39) : error C2248: 'Pen' : cannot access private member declared in class 'worker'
c:usersadministratordesktopss.cpp(10) : see declaration of 'Pen'
c:usersadministratordesktopss.cpp(41) : error C2248: 'Age' : cannot access private member declared in class 'worker'
c:usersadministratordesktopss.cpp(11) : see declaration of 'Age'
c:usersadministratordesktopss.cpp(43) : error C2248: 'Sal' : cannot access private member declared in class 'worker'
c:usersadministratordesktopss.cpp(12) : see declaration of 'Sal'
c:usersadministratordesktopss.cpp(45) : error C2248: 'Num' : cannot access private member declared in class 'worker'
c:usersadministratordesktopss.cpp(8) : see declaration of 'Num'
c:usersadministratordesktopss.cpp(45) : error C2248: 'Nam' : cannot access private member declared in class 'worker'
c:usersadministratordesktopss.cpp(9) : see declaration of 'Nam'
c:usersadministratordesktopss.cpp(45) : error C2248: 'Pen' : cannot access private member declared in class 'worker'
c:usersadministratordesktopss.cpp(10) : see declaration of 'Pen'
c:usersadministratordesktopss.cpp(45) : error C2248: 'Age' : cannot access private member declared in class 'worker'
c:usersadministratordesktopss.cpp(11) : see declaration of 'Age'
c:usersadministratordesktopss.cpp(45) : error C2248: 'Sal' : cannot access private member declared in class 'worker'
c:usersadministratordesktopss.cpp(12) : see declaration of 'Sal'
c:usersadministratordesktopss.cpp(123) : error C2593: 'operator >>' is ambiguous
解决方案
pricvaye变量不能访问,用public改成
解决方案二:
class worker{
public: //加上这个
...