C++新手求助,求帮忙看看这段代码错在哪里、应该如何修改,谢谢!

//给两个点poi1(x1,y1),poi(x2,y2),求它们所确定的矩形的面积area。#include<iostream>#include<cmath>#defin... //给两个点poi1(x1,y1),poi(x2,y2),求它们所确定的矩形的面积area。
#include <iostream>
#include <cmath>
#define ABS(x) (x)>0?(x):-(x)
using namespace std;
class Point
{
public:
Point (int xx,int yy)
{ x=xx;
y=yy;
}
Point (Point &p);
int getx() {return x;}
int gety() {return y;}
private:
int x,y;
}
Point::Point (Point &p)
{ x=p.x;
y=p.y;
}
class Rectangle
{
public:
Rectangle (Point sp1,Point sp2);
Rectangle (Rectangle &a);
int gets() {return s;}
private:
Point p1,p2;
int s;
}
Rectangle::Rectangle (Point sp1,Point sp2):p1(sp1),p2(sp2)
{ int len=ABS(sp1.getx()-sp2.getx());
int wid=ABS(sp1.gety()-sp2.gety());
s=len*wid;
}
Rectangle::Rectangle (Rectangle &a):p1(a.p1),p2(a.p2)
{ s=a.s;
}
int main()
{ int x1,y1,x2,y2;
cout<<"Please enter the set of the first point:";
cin>>x1>>y1;
cout<<"Please enter the set of the second point:";
cin>>x2>>y2;
Point poi1(x1,y1),poi2(x2,y2);
Rectangle area(poi1,poi2);
cout<<"The area of the rectangle is:"<<area.gets()<<endl;
return 0;
}
编译结果:
--------------------Configuration: Rectangle - Win32 Debug--------------------
Compiling...
Rectangle.cpp
F:\study\VC++\Rectangle\Rectangle.cpp(19) : error C2533: 'Point::Point' : constructors not allowed a return type
F:\study\VC++\Rectangle\Rectangle.cpp(32) : error C2533: 'Rectangle::Rectangle' : constructors not allowed a return type
F:\study\VC++\Rectangle\Rectangle.cpp(33) : error C2264: 'Point::Point' : error in function definition or declaration; function not called
F:\study\VC++\Rectangle\Rectangle.cpp(33) : error C2264: 'Point::Point' : error in function definition or declaration; function not called
F:\study\VC++\Rectangle\Rectangle.cpp(38) : error C2264: 'Point::Point' : error in function definition or declaration; function not called
F:\study\VC++\Rectangle\Rectangle.cpp(38) : error C2264: 'Point::Point' : error in function definition or declaration; function not called
F:\study\VC++\Rectangle\Rectangle.cpp(47) : error C2264: 'Rectangle::Rectangle' : error in function definition or declaration; function not called
执行 cl.exe 时出错.

求各位前辈帮帮忙,刚接触类的概念,用的非常生疏。。。

Rectangle.obj - 1 error(s), 0 warning(s)
展开
 我来答
lyhdez1
2012-03-28 · TA获得超过1942个赞
知道小有建树答主
回答量:687
采纳率:100%
帮助的人:841万
展开全部
程序写的很不错,但要记住一点,类定义完了之后,是有分号的,否则报错,而且是些莫名其妙的错误。养成好习惯,很关键。
//给两个点poi1(x1,y1),poi(x2,y2),求它们所确定的矩形的面积area。
#include <iostream>
#include <cmath>
#define ABS(x) (x)>0?(x):-(x)
using namespace std;
class Point
{
public:
Point (int xx,int yy)
{ x=xx;
y=yy;
}
Point (Point &p);
int getx() {return x;}
int gety() {return y;}
private:
int x,y;
};//少了分号
Point::Point (Point &p)
{ x=p.x;
y=p.y;
}
class Rectangle
{
public:
Rectangle (Point sp1,Point sp2);
Rectangle (Rectangle &a);
int gets() {return s;}
private:
Point p1,p2;
int s;
};//少了分号
Rectangle::Rectangle (Point sp1,Point sp2):p1(sp1),p2(sp2)
{ int len=ABS(sp1.getx()-sp2.getx());
int wid=ABS(sp1.gety()-sp2.gety());
s=len*wid;
}
Rectangle::Rectangle (Rectangle &a):p1(a.p1),p2(a.p2)
{ s=a.s;
}
int main()
{ int x1,y1,x2,y2;
cout<<"Please enter the set of the first point:";
cin>>x1>>y1;
cout<<"Please enter the set of the second point:";
cin>>x2>>y2;
Point poi1(x1,y1),poi2(x2,y2);
Rectangle area(poi1,poi2);
cout<<"The area of the rectangle is:"<<area.gets()<<endl;
return 0;
}
andy6355
2012-03-28 · TA获得超过117个赞
知道小有建树答主
回答量:77
采纳率:0%
帮助的人:76.9万
展开全部
Point和Rectangle两个class的声明后面都没分号。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
zilong10165515
2012-03-28
知道答主
回答量:14
采纳率:0%
帮助的人:15.3万
展开全部
少了分号
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式