nine_forever 2020-06-11 13:08 采纳率: 0%
浏览 509
已采纳

为什么构造函数不含有默认参数后就提示默认构造函数已被删除?

#include<iostream>
#include<math.h>
using namespace std;

class myPoint {
public:
    myPoint(double x0  , double y0 ) :x(x0), y(y0) {}
    myPoint(myPoint& np) :x(np.x), y(np.y) {}
    //得到X或Y坐标
    double GetX() { return x; }
    double GetY() { return y; }
    //设置X或Y坐标
    void SetX(double x0) { x = x0; }
    void SetY(double y0) { y = y0; }
    //设置点坐标
    void SetPoint(double x0, double y0) { x = x0; y = y0; }
    void SetPoint(myPoint& np) { x = np.x; y = np.y; }
    //计算三角形边长
    double  GetLength(myPoint p) {
        return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
    }
    void Printit() { cout << " (" << x << "," << y << ") "; }
private:
    double x, y;
};

class Triangle
{
public:
    //计算三角形面积
    double area(double a, double b, double c);
    //计算三角形周长
    double perimeter(double a, double b, double c)
    {
        return a + b + c;
    }
    myPoint p1, p2, p3;
};

double Triangle::area(double a, double b, double c)
{
    double p = (a + b + c) / 2;
    return sqrt(p * (p - a) * (p - b) * (p - c));
}

int main()
{
    Triangle t1;
    t1.p1.SetPoint(0.0, 0.0);
    t1.p2.SetPoint(4.0, 0.0);
    t1.p3.SetPoint(0.0, 3.0);
    cout << "The triangle's area is  " << t1.area(t1.p1.GetLength(t1.p3), t1.p2.GetLength(t1.p1), t1.p2.GetLength(t1.p3)) << endl;
    cout << "The triangle's area is  " << t1.perimeter(t1.p1.GetLength(t1.p3), t1.p2.GetLength(t1.p1), t1.p2.GetLength(t1.p3)) << endl;
    return 0;
}



图片说明

在给构造函数的每个形参都加上默认参数就好了,请问是什么原因

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-06-11 17:13
    关注

    因为没有无参数构造函数

    要么:

    myPoint(double x0 , double y0 ) :x(x0), y(y0) {}
    ->
    myPoint(double x0 =0 , double y0 =0) :x(x0), y(y0) {}

    要么加上

    myPoint() :x(0), y(0) {}

    问题解决的话,请点下采纳

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!