鱼C论坛

 找回密码
 立即注册
查看: 850|回复: 1

C++的问题,求助

[复制链接]
发表于 2024-6-9 16:45:25 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<iostream>
  2. using namespace std;

  3. class Singer
  4. {
  5.         protected:
  6.                 string name;
  7.                 char sex;
  8.                 int age;
  9.                 double score;
  10.         public:
  11.                 string getName() const{ return name; };
  12.                 Singer(string name="", char s='?' ,int a=0,double n=0 );//构造函数要写的
  13.                 friend istream& operator >> (istream &, Singer &);
  14.                 bool operator >(Singer & a) ;
  15.                 bool operator ==(Singer &b) ;
  16.                 friend ostream& operator << (ostream &, const Singer &);
  17. };
  18. Singer::Singer(string name, char s, int a,double n )
  19. {
  20.     this->name= name;
  21.     score = n;
  22.     sex = s;
  23.     age = a;
  24. }
  25. istream& operator >> (istream & myin, Singer & sin)
  26. {
  27.         myin >> sin.name >> sin.sex >> sin.age >> sin.score;
  28.         return myin;
  29. }
  30. ostream& operator << (ostream & myout, const Singer & sing)
  31. {
  32.         myout << sing.name << sing.sex << sing.age << sing.score;
  33.         return myout;
  34. }
  35. bool operator >(Singer & a)
  36. {
  37.         if(this->score > a.score)
  38.         return true;
  39.         else
  40.         return false;
  41. }
  42. bool operator ==(Singer &b)
  43. {
  44.         if(this->score == b.score)
  45.         return true;
  46.         else
  47.         return false;       
  48. }

  49. int main()
  50. {
  51. Singer s1,s2;
  52. cin>>s1>>s2;
  53. cout<<s1<<"\n"<<s2<<endl;
  54. if(s1>s2)
  55. cout<<s1.getName()<<"'s score is higher than "<<s2.getName()<<"'s.\n";

  56. else if(s1==s2)
  57. cout<<s1.getName()<<"'s score is equal to "<<s2.getName()<<"'s.\n";

  58. else
  59. cout<<s1.getName()<<"'s score is lower than "<<s2.getName()<<"'s.\n";

  60. return 0;

  61. }
复制代码

//bool的地方出了问题,但我不知道哪里有问题,感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-6-9 16:45:36 | 显示全部楼层
你好!首先,你的代码中缺少了头文件的引用,应该加上`#include <iostream>`。

接下来是构造函数的实现部分。构造函数用于初始化对象的成员变量。根据你的代码,构造函数应该是这样的:

  1. Singer::Singer(string n, char s, int a, double sc)
  2. {
  3.     name = n;
  4.     sex = s;
  5.     age = a;
  6.     score = sc;
  7. }
复制代码


在这里,我们使用了参数列表来初始化成员变量。

接下来是比较运算符的重载部分。根据你的代码,重载的比较运算符应该是这样的:

  1. bool Singer::operator>(Singer& a)
  2. {
  3.     return score > a.score;
  4. }

  5. bool Singer::operator==(Singer& b)
  6. {
  7.     return score == b.score;
  8. }
复制代码


这里我们使用了成员函数的方式来重载比较运算符。

最后,是输出流运算符的重载部分。按照你的代码,重载的输出流运算符应该是这样的:

  1. ostream& operator<<(ostream& os, Singer& sin)
  2. {
  3.     os << sin.name << " " << sin.sex << " " << sin.age << " " << sin.score;
  4.     return os;
  5. }
复制代码


这里我们使用了friend函数的方式来重载输出流运算符。

完整的代码如下所示:

  1. #include <iostream>
  2. using namespace std;

  3. class Singer
  4. {
  5. protected:
  6.     string name;
  7.     char sex;
  8.     int age;
  9.     double score;

  10. public:
  11.     Singer(string name = "", char s = '?', int a = 0, double n = 0);
  12.     string getName() const { return name; };
  13.     friend istream& operator>>(istream&, Singer&);
  14.     bool operator>(Singer& a);
  15.     bool operator==(Singer& b);
  16.     friend ostream& operator<<(ostream&, Singer&);
  17. };

  18. Singer::Singer(string n, char s, int a, double sc)
  19. {
  20.     name = n;
  21.     sex = s;
  22.     age = a;
  23.     score = sc;
  24. }

  25. bool Singer::operator>(Singer& a)
  26. {
  27.     return score > a.score;
  28. }

  29. bool Singer::operator==(Singer& b)
  30. {
  31.     return score == b.score;
  32. }

  33. istream& operator>>(istream& myin, Singer& sin)
  34. {
  35.     myin >> sin.name >> sin.sex >> sin.age >> sin.score;
  36.     return myin;
  37. }

  38. ostream& operator<<(ostream& os, Singer& sin)
  39. {
  40.     os << sin.name << " " << sin.sex << " " << sin.age << " " << sin.score;
  41.     return os;
  42. }

  43. int main()
  44. {
  45.     Singer s1, s2;
  46.     cin >> s1 >> s2;
  47.     cout << s1 << endl;
  48.     cout << s2 << endl;

  49.     if (s1 > s2)
  50.     {
  51.         cout << "s1的得分大于s2" << endl;
  52.     }
  53.     else if (s1 == s2)
  54.     {
  55.         cout << "s1和s2的得分相等" << endl;
  56.     }
  57.     else
  58.     {
  59.         cout << "s1的得分小于s2" << endl;
  60.     }

  61.     return 0;
  62. }
复制代码


希望能帮到你!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-5-10 12:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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