这个程序错在哪里呀~~~求高手解答,在线等答案

#include<iostream.h>
class Rect{
private:
float x;
float y;
float w;
float h;
public:
Rect(){x=0;y=0,w=0;h=0;};
Rect(float a,float b,float c,float d)
{x=a;y=b;w=c;h=d;};
Rect operator+(Rect b);
void Display();
};
Rect operator+(Rect b)
{
Rect s;
s.x=x+s.x;
s.y=y+s.y;s.w=w+s.w;s.h=h+s.h;
return s;
}
void Rect::Display()
{
cout<<"x="<<x<<'\t'<<"y="<<y<<'\t';
cout<<"w="<<w<<'\t'<<"h="<<h<<'\t';
}
void main()
{
Rect A,B(1.4,2,3,20),C(2.5,5,3,4.8);
A=B+C;
A.Display();
}

Rect operator+(Rect b)这个函数错了 改成Rect Rect::operator+(Rect b) 编译通过追问

好像还是不对,不能使两矩形想加

追答

问题调试下就可以出来的啦
Rect Rect::operator+(Rect b)
{
Rect s;
s.x=x+b.x;
s.y=y+b.y;s.w=w+b.w;s.h=h+b.h;
return s;
}这样子 答案正确 满意了吧

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答