鱼C论坛

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

[已解决]二元运算符重载 参数出错

[复制链接]
发表于 2017-12-7 16:52:11 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cstring>
  4. using namespace std;

  5. class Student
  6. {
  7.         friend int operator-(int, Student);

  8. private:
  9.         char Name[20];
  10.         int Score;
  11. public:
  12.         Student(char *N,int s)
  13.         {
  14.                 strcpy(Name , N);
  15.                 Score = s;
  16.         }
  17.         bool operator>(Student b)
  18.         {
  19.                 if (this->Score > b.Score)
  20.                         return true;
  21.                 else
  22.                         return false;
  23.         }
  24.         void ShowName(void) { cout << "名字=" << Name << endl; }
  25.         void ShowName(void) { cout << "成绩=" << Score << endl; }
  26. };
  27. int operator-(int p, Student q)
  28. {
  29.         return (p - q.Score);
  30. }

  31. int main()
  32. {
  33.         Student x("yuanyangxin", 70);
  34.         Student y("sasds", 80);

  35.         x.ShowName();
  36.        
  37.         system("pause");
  38.         return 0;
  39. }
复制代码



1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppBuild.targets(392,5): warning MSB8028: The intermediate directory (Debug\) contains files shared from another project (test.vcxproj).  This can lead to incorrect clean and rebuild behavior.
1>  源.cpp
1>d:\workspace\vs2015\test\test\源.cpp(14): error C2146: 语法错误: 缺少“)”(在标识符“s”的前面)
1>d:\workspace\vs2015\test\test\源.cpp(14): error C3646: “s”: 未知重写说明符
1>d:\workspace\vs2015\test\test\源.cpp(14): error C2059: 语法错误:“)”
1>d:\workspace\vs2015\test\test\源.cpp(27): error C2535: “void Student::ShowName(void)”: 已经定义或声明成员函数
1>  d:\workspace\vs2015\test\test\源.cpp(26): note: 参见“Student::ShowName”的声明
1>d:\workspace\vs2015\test\test\源.cpp(16): error C2065: “N”: 未声明的标识符
1>d:\workspace\vs2015\test\test\源.cpp(17): error C2065: “s”: 未声明的标识符
1>d:\workspace\vs2015\test\test\源.cpp(36): error C2661: “Student::Student”: 没有重载函数接受 2 个参数
1>d:\workspace\vs2015\test\test\源.cpp(37): error C2661: “Student::Student”: 没有重载函数接受 2 个参数
最佳答案
2017-12-7 16:58:25
14行,中文逗号
重载ShowName函数,形参不能一模一样
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-12-7 16:58:25 | 显示全部楼层    本楼为最佳答案   
14行,中文逗号
重载ShowName函数,形参不能一模一样
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 23:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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