c++error C2512: 'Rectangle' : no appropriate default constructor available

来源:百度知道 编辑:UC知道 时间:2024/06/22 03:38:59
源代码是:#include<iostream.h>
#include<math.h>
class Point
{
public:
Point();
Point(double x1,double y1);
~Point();
double Abs();
double Getpoitnx()
{
return x;
}
double Getpoitny()
{
return y;
}
private:
double x,y;
};

class Rectangle
{
public:
Rectangle(Point p_1,Point p_2);
bool Is_rectangle();
void display();//从左上角开始逆时针显示矩形的各个点的x和y坐标
void area();
bool Is_square();
private:
Point p1,p2;
};

Point:: Point()
{
x=0;
y=0;
cout<<"Executing No Parameter Constructor\\"<<endl;
}
Point::Point(double x1,double y1)
{
x=x1;
y=y1;
cout<<"Executing Constructor"<<endl;
}
Point::~Point()
{
cout<<"Executing Constructor"<<endl;
}
dou

编译器提示的是没有合适的默认构造函数
第一是这里:Point p[3]={Point(3,4),Point(12,18)};
第二在Rectangle 里你没有定义一个参数的构造函数,但却调用Rectangle (r0); 定一个吧~~~

声明时少了一个对象,就是对象数组那里
Rectangle的构造函数不能被调用,因为你的构造函数中没有设定一个参数的构造函数,这样r0不能调用构造函数,而系统又没有找到,所以要自己设定一个重载构造函数,形参(Point x)这样的构造函数

Rectangle这个类的构造函数 有四个重载的 但没有一个重载的是 传进两个点,但它需要的确实是两个点 但不是这样传 :而是Rectangle(int x1,int y1,int x2,int y2)

其中(x1,y1)是矩形左上角的坐标 (x2,y2)是右下角的坐标。