鱼C论坛

 找回密码
 立即注册
查看: 3148|回复: 3

关于C++运算符重载 问题

[复制链接]
发表于 2012-7-17 21:50:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<iostream>

  2. using namespace std;

  3. class String
  4. {
  5. public:
  6.         String();
  7.         String(char *str);
  8.         friend bool operator>(String &string1, String &string2);
  9.         friend bool operator==(String &string1, String &string2);
  10.         friend bool operator<(String &string1, String &string2);
  11.         void display();
  12. private:
  13.         char *p;
  14. };

  15. void compare(String &string1, String &string2);

  16. int main()
  17. {
  18.         String s1("libin");
  19.         String s2("chengbaoshu");
  20.         String s3("abit");
  21.         String s4("libin");

  22.         compare(s1,s2);
  23.         compare(s4,s3);
  24.         compare(s1,s4);

  25.         cout<<endl;

  26.         return 0;
  27. }

  28. String::String()
  29. {
  30.         p=NULL;
  31. }

  32. String::String(char *str):p(str){}

  33. bool operator>(String &string1, String &string2)                //下面定义的三个 运算符重载函数都是出同一个错误
  34. {
  35.         if(strcmp(string1.p,string2.p)>0)                        //就是我定义的是 友元函数 但是为什么不能访问String类的 私有成员p?
  36.                 return true;
  37.         else
  38.                 return false;
  39. }

  40. bool operator>(String &string1, String &string2)
  41. {
  42.         if(strcmp(string1.p,string2.p)<0)
  43.                 return true;
  44.         else
  45.                 return false;
  46. }


  47. bool operator>(String &string1, String &string2)
  48. {
  49.         if(strcmp(string1.p,string2.p)==0)
  50.                 return true;
  51.         else
  52.                 return false;
  53. }

  54. void String::display()
  55. {
  56.         cout<<p;
  57. }

  58. void compare(String &string1, String &string2)
  59. {
  60.         if(operator>(string1,string2)==1)                //">"为什么这里说 我 含糊的 调用 函数
  61.         {
  62.                 string1.display();
  63.                 cout<<">";
  64.                 string2.display();
  65.                 cout<<endl;
  66.         }
  67.         else
  68.         {
  69.                 if(operator==(string1,string2)==1)
  70.                 {
  71.                         string1.display();
  72.                         cout<<"==";
  73.                         string2.display();
  74.                         cout<<endl;
  75.                 }
  76.                 else
  77.                 {
  78.                         if(operator<(string1,string2)==1)
  79.                         {
  80.                                 string1.display();
  81.                                 cout<<"<";
  82.                                 string2.display();
  83.                                 cout<<endl;
  84.                         }
  85.                 }
  86.         }
  87. }
复制代码
求解答
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-7-18 08:39:14 | 显示全部楼层
  1. #include<iostream>

  2. using namespace std;

  3. //由于VC6不标准,使用友元必须加上以下3行
  4. class String;
  5. bool operator>(String &string1, String &string2);
  6. bool operator==(String &string1, String &string2);
  7. bool operator<(String &string1, String &string2);
  8. //由于VC6不标准,使用友元必须加上以上3行

  9. class String
  10. {
  11. public:
  12.         String();
  13.         String(char *str);
  14.         friend bool operator>(String &string1, String &string2);
  15.         friend bool operator==(String &string1, String &string2);
  16.         friend bool operator<(String &string1, String &string2);
  17.         void display();
  18. private:
  19.         char *p;
  20. };

  21. void compare(String &string1, String &string2);

  22. int main()
  23. {
  24.         String s1("libin");
  25.         String s2("chengbaoshu");
  26.         String s3("abit");
  27.         String s4("libin");

  28.         compare(s1,s2);
  29.         compare(s4,s3);
  30.         compare(s1,s4);

  31.         cout<<endl;

  32.         return 0;
  33. }

  34. String::String()
  35. {
  36.         p=NULL;
  37. }

  38. String::String(char *str):p(str){}

  39. bool operator>(String &string1, String &string2)                //下面定义的三个 运算符重载函数都是出同一个错误
  40. {
  41.         if(strcmp(string1.p,string2.p)>0)                        //就是我定义的是 友元函数 但是为什么不能访问String类的 私有成员p?
  42.                 return true;
  43.         else
  44.                 return false;
  45. }

  46. bool operator<(String &string1, String &string2)
  47. {
  48.         if(strcmp(string1.p,string2.p)<0)
  49.                 return true;
  50.         else
  51.                 return false;
  52. }


  53. bool operator==(String &string1, String &string2)
  54. {
  55.         if(strcmp(string1.p,string2.p)==0)
  56.                 return true;
  57.         else
  58.                 return false;
  59. }

  60. void String::display()
  61. {
  62.         cout<<p;
  63. }

  64. void compare(String &string1, String &string2)
  65. {
  66.         if(operator>(string1,string2)==1)                //">"为什么这里说 我 含糊的 调用 函数
  67.         {
  68.                 string1.display();
  69.                 cout<<">";
  70.                 string2.display();
  71.                 cout<<endl;
  72.         }
  73.         else
  74.         {
  75.                 if(operator==(string1,string2)==1)
  76.                 {
  77.                         string1.display();
  78.                         cout<<"==";
  79.                         string2.display();
  80.                         cout<<endl;
  81.                 }
  82.                 else
  83.                 {
  84.                         if(operator<(string1,string2)==1)
  85.                         {
  86.                                 string1.display();
  87.                                 cout<<"<";
  88.                                 string2.display();
  89.                                 cout<<endl;
  90.                         }
  91.                 }
  92.         }
  93. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2012-7-18 08:46:00 | 显示全部楼层
明白了  谢谢  
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-7-18 09:43:48 | 显示全部楼层
不知道你写类为什么不生成单独文件
h文件写声明 cpp写定义
这样不需要项前声明的
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-14 05:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表