鱼C论坛

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

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

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

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

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

x
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;

class Student
{
        friend int operator-(int, Student);

private:
        char Name[20];
        int Score;
public:
        Student(char *N,int s)
        {
                strcpy(Name , N);
                Score = s;
        }
        bool operator>(Student b)
        {
                if (this->Score > b.Score)
                        return true;
                else
                        return false;
        }
        void ShowName(void) { cout << "名字=" << Name << endl; }
        void ShowName(void) { cout << "成绩=" << Score << endl; }
};
int operator-(int p, Student q)
{
        return (p - q.Score);
}

int main()
{
        Student x("yuanyangxin", 70);
        Student y("sasds", 80);

        x.ShowName();
        
        system("pause");
        return 0;
}


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-10-1 03:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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