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) 展开
#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) 展开
展开全部
程序写的很不错,但要记住一点,类定义完了之后,是有分号的,否则报错,而且是些莫名其妙的错误。养成好习惯,很关键。
//给两个点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;
}
//给两个点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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询