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) {}

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

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

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集