C++重载运算符问题

aqqpz 2009-04-12 09:26:41
#include<iostream>
#include<string.h>
using namespace std;

class person
{
private:
int id;
char name[50];
int office;
int nowhour;
public:
void show_nowhour(void)
{
cout<<endl;
cout<<name<<" has worked "<<nowhour <<" hours"<<endl;
}

//default constructor
person()
{
id=0; strcpy(name,"Not known"); office=123; nowhour=0;
}

//normal constructor
person(int id_in, char name_in[50], int office_in,
int nowhour_in)

{
id=id_in;
strcpy(name,name_in);
office=office_in;
nowhour=nowhour_in;
}

void changename(void)
{
char name0[100];
cout<<"Please input new name"<<endl;
cin.getline(name0,100);
strcpy(name,name0);
cout<<"Action finished with " <<name<<endl;
}
void changeid(void)
{
int value1;
cout<<"Please input new value for id"<<endl;
cin>>value1;
id=value1;
cout<<"Action finished with " <<name<<endl;
}
void changeoffice(void)
{
int value2;
cout<<"Please input new value for office"<<endl;
cin>>value2;
cin.ignore();
office=value2;
cout<<"Action finished with " <<name<<endl;
}
void changenowhour(void)
{
int value;
cout<<"Please input new value for nowhour"<<endl;
cin>>value;
cin.ignore();
nowhour=value;
cout<<"Action finished with " <<name<<endl;
}

//a function that asks a user to perform action on its data
void action(void)
{
int choice;
cout<<"what do you want to do with "<<name<<endl;
cout<<" 1 for display, 2 for changing hours of working,3 for changing name, 4 for change id, 5 for changing office"<<endl;
cin>>choice;
cin.ignore();
if(choice==1)
show_nowhour( );
if(choice==2)
changenowhour( );
if(choice==3)
changename();
if (choice==4)
changeid();
if(choice==5)
changeoffice();
}

bool operator==(person & aperson)
{
return this->name( ) ==aperson.name( );

}

};
// a class to handle two objects of CLASS person
class record
{
private:
person p1,p2,p3;


public:
void run(void)
{
int userchoice=1;
while(userchoice>0 && userchoice<4)
{
cout<<"input your choice: 1 for p1, 2 for p2, 3 for p3;4 to exit"<<endl;
cin>>userchoice;
cin.ignore();
if (userchoice==1) p1.action();
if (userchoice==2) p2.action();
if (userchoice==3) p3.action();

if(p1==p2)
cout<<"p1 and p2 are the same."<<endl;
if(p1==p3)
cout<<"p1 and p3 are the same."<<endl;
if(p3==p2)
cout<<"p3 and p2 are the same."<<endl;
}
}
};
//main programme follows all class definition

void main( void )
{
record r1;
r1.run( );
}

原题是
4. Add more functions to the person class so for each person, options should be available to the user to display or change the name, ID number and office number (through the action( ) function of the class person). The operator == should be overloaded for the class person so it can be used to check if two persons are identical (returning a logical true or false), i.e. all the corresponding data members of the two persons have the same values.
5. Add more functions to the person class and modify the record class so that the programme will be able to deal with 3 persons. There should also be options in the run( ) function of the record class to allow comparison of any two persons to see if they are identical.
对于
bool operator==(person & aperson)
{
return this->name( ) ==aperson.name( );

}
我的这个程序有错误:项不会计算为接受 0 个参数的函数
请各位高手帮我看些还有其他问题不,谢谢
急!!!!!!!!!!!

...全文
105 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ypb362148418 2009-04-13
  • 打赏
  • 举报
回复
麻烦贴上那一块出问题了
hbwgift 2009-04-13
  • 打赏
  • 举报
回复
改成:
bool operator==(person & aperson)
{
return this->name == aperson.name;

}
试试。
liujianwish 2009-04-13
  • 打赏
  • 举报
回复
晕,发现name是成员变量。。。而且是char*的。。。怎么可以用==来比较呢……要用strcmp,貌似使这个函数,用来比较字符串的
liujianwish 2009-04-13
  • 打赏
  • 举报
回复
我晕xjtlu的。请查看name函数的返回类型,因为你比较的是函数的返回值。是void或char的话可能会失败
hiboys 2009-04-13
  • 打赏
  • 举报
回复
重载了==就不要放过!=
最好一起重载。
否则你的person千万别加入【容器】内使用。切记!!
firsthym 2009-04-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 liujianwish 的回复:]
晕,发现name是成员变量。。。而且是char*的。。。怎么可以用==来比较呢……要用strcmp,貌似使这个函数,用来比较字符串的
[/Quote]
非常正确,你直接用==,是比较的两个的指针地址,这永远不相等的,至少实际情况是。
ericming200409 2009-04-12
  • 打赏
  • 举报
回复
bool operator==(person & aperson)
{
return this->name( ) ==aperson.name( );

}
哪里来的name()??
lq651659889 2009-04-12
  • 打赏
  • 举报
回复
英文的。。。。。帮顶

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧