编译问题

[复制链接]
1741|5
 楼主| txcy 发表于 2014-2-28 19:05 | 显示全部楼层 |阅读模式
#include <iostream>
using namespace std;
class Person{
public:
    int id;
    Person(int id){
        this->id=id;
    }
    void toString(){
        cout<<"{id:"<<this->id<<"}";
    }
     operator ==(const Person &p1,const Person &p2){
        return p1.id==p2.id?true:false;
    }
     operator !=(const Person &p1,const Person &p2){
             return p1.id==p2.id?false:true;
         }
};
VC6.0中报错了:
error C2804: binary 'operator ==' has too many parameters
error C2333: '==' : error in function declaration; skipping function body
error C2804: binary 'operator !=' has too many parameters
error C2333: '!=' : error in function declaration; skipping function body

可是我试过了以下方法,还是不通过:
operator ==(const Person &p2){
        return id==p2.id?true:false;
    }
该怎么办呢?
秋天落叶 发表于 2014-2-28 19:16 | 显示全部楼层
需要带个bool的返回值
pkat 发表于 2014-2-28 19:24 | 显示全部楼层
要么声明为友元,要么减少一个参数,二目运算符声明为类的成员函数时,只有一个参数。
baidudz 发表于 2014-2-28 19:33 | 显示全部楼层
参数太多了,错误已经给出提示了!!!
smilingangel 发表于 2014-2-28 22:37 | 显示全部楼层
too many parameters?看下==右边的变量参数的,能不能先计算好的,在直接用的呢
香水橙 发表于 2014-3-1 21:45 | 显示全部楼层
加上括号:return (p1.id==p2.id)?true:false;
您需要登录后才可以回帖 登录 | 注册

本版积分规则

274

主题

2106

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部