| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 396 人关注过本帖
标题:关于运算符重载,不知何处问题,多谢指教
只看楼主 加入收藏
weinikuaile
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-5-28
结帖率:0
收藏
已结贴  问题点数:20 回复次数:5 
关于运算符重载,不知何处问题,多谢指教
#include <iostream>
using namespace std;
class Point{
private:
    int x;
    int y;
public:
   Point operator+(Point a,Point b);
   Point operator=(Point a,Point b);
   Point(){this->x=0;this->y=0;}
   Point(int a,int b):x(a),y(b){}
   void display();

};
Point Point::operator+(Point a,Point b){
            a.x+=b.x;
            a.y+=b.y;
            return a;
}
Point Point::operator=(Point a,Point b){
    a.x=b.x;
    a.y=b.y;
    return a;
}
void Point::display(){
    cout<<"("<<this->x<<","<<this->y<<")"<<endl;
}
int main(){
    Point point1(1,2),point2(2,1);
    Point point3=point1+point2;
    point3.display();
    return 0;

}error C2804: binary 'operator +' has too many parameters
error C2804: binary 'operator =' has too many parameters
搜索更多相关主题的帖子: void private display include public 
2012-05-28 17:22
跳跳鱼
Rank: 2
等 级:论坛游民
帖 子:74
专家分:60
注 册:2011-5-4
收藏
得分:20 
回复 楼主 weinikuaile
下面的这个是正确的
程序代码:
#include <iostream>
using namespace std;
class Point{
private:
    int x;
    int y;
public:
   Point operator+(Point &a);
   Point operator=(Point &a);
   Point(){this->x=0;this->y=0;}
   Point(int a,int b):x(a),y(b){}
   void display();

};
Point Point::operator+(Point &a){
            Point b;
            b.x=x+b.x;
            b.y=y+b.y;
            return b;
}
Point Point::operator=(Point &a){
    Point b;
    b.x=b.x;
    b.y=b.y;
    return b;
}
void Point::display(){
    cout<<"("<<this->x<<","<<this->y<<")"<<endl;
}
int main(){
    Point point1(1,2),point2(2,1);
    Point point3=point1+point2;
    point3.display();
    return 0;

}

编译提示信息意思是Point类参数太多
2012-05-28 19:21
weinikuaile
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-5-28
收藏
得分:0 
以下是引用跳跳鱼在2012-5-28 19:21:01的发言:

下面的这个是正确的
#include  
using namespace std;
class Point{
private:
    int x;
    int y;
public:
   Point operator+(Point &a);
   Point operator=(Point &a);
   Point(){this->x=0;this->y=0;}
   Point(int a,int b):x(a),y(b){}
   void display();
 
};
Point Point::operator+(Point &a){
            Point b;
            b.x=x+b.x;
            b.y=y+b.y;
            return b;
}
Point Point::operator=(Point &a){
    Point b;
    b.x=b.x;
    b.y=b.y;
    return b;
}
void Point::display(){
    cout<<"("<x<<","<y<<")"<
谢谢,可以这样做,结果却不对哦。我再看看。
2012-05-29 04:16
weinikuaile
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-5-28
收藏
得分:0 
当运算符重载为类的成员函数时,函数的参数个数比原来的操作个数要少一个;当重载为类的友元函数时,函数的参数个数与原操作个数相同。原因是重载为类的成员函数时,如果某个对象使用重载了的成员函数,自身的数据可以直接访问,就不需要再放在参数表中进行传递,少了的操作数就是该对象本身。而重载为友元函数时,友元函数对某个对象的数据进行操作,就必须通过该对象的名称来进行,因此使用到的参数都要进行,因此使用到的参数都要进行传递,操作的个数就不会有变化。
2012-05-29 04:37
weinikuaile
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-5-28
收藏
得分:0 
程序代码:
#include <iostream>
using namespace std;
class Point{
private:
    int x;
    int y;
public:
    Point operator+(Point &a);
    Point operator=(Point &a);
    Point(){this->x=0;this->y=0;}
    Point(int a,int b):x(a),y(b){}
    void display();
   
};
Point Point::operator+(Point &a){//Point &a等价于+后面的参数point2
    Point b;//定义了b,
    x=x+a.x;
    y=y+a.y;
    return Point(x,y);//此处原来为return b;如此则错误,对象b没有参与到运算当中哦,因此返回值将是(0,0)
}
Point Point::operator=(Point &a){
    Point b;
    x=a.x;
    y=a.y;
    return Point(x,y);
}
void Point::display(){
    cout<<"("<<this->x<<","<<this->y<<")"<<endl;
}
int main(){
    Point point1(1,2),point2(2,1);
    Point point3=point1+point2;
    point3.display();
    return 0;
   
}
2012-05-29 04:59
weinikuaile
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-5-28
收藏
得分:0 
这样就正确了,呵呵,还是谢谢哦。
2012-05-29 05:00
快速回复:关于运算符重载,不知何处问题,多谢指教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016457 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved