qq_45735316 2020-03-25 17:23 采纳率: 94.1%
浏览 317
已采纳

为什么无法调用构造函数?代码应该怎样修改?

图片说明
/*5、编写C++程序,实现如下功能:
a.定义一个类,Point, 具有x, y轴坐标两个私有数据成员(float类型);
b.具有获取和设置x,y值的公有函数;写出两种构造函数;其原型为:
Point();//此时初始化x,y为0
Point(float xx,float yy);
c.具有计算与另一个类对象p1之间距离的公有函数:float calculate_distance(Point p1);
d. 在主函数中,提示输入p1, p2两点的x,y坐标,并计算两点的距离;
e.在main函数中测试你所设计的所有成员函数。*/

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
class Point{
    public:
        Point(){
            x=0;
            y=0;
        }                  //无参数的构造函数 
        Point(float xx,float yy);  //有参数的构造函数 
        float calculate_distance(Point p1);
    private:
        float x,y;
};
Point::Point(float xx,float yy){
            x=xx;
            y=yy;
            cout<<"("<<x<<","<<y<<")"<<endl;
}
float Point::calculate_distance(Point p1){
            return (float)sqrt((x-p1.x)*(x-p1.x)+(y-p1.y)*(y-p1.y));

}
int main(int argc, char** argv) {
    Point p,p1;     //定义对象p、p1 
    float x,y;      //对象p的私有数据成员 
    float x1,y1;    //对象p1的私有对象成员
    float d1=0,d2=0;
    cin>>x>>y;
    cin>>x1>>y1;
    Point p;         //调用无参数的构造函数 
    Point p(x,y);    //调用有参数的构造函数 
    p1.Point(x1,y1);
    d1=p.calculate_distance(p1);
    d2=p1.calculate_distance(p);
    cout<<"两点间距离为"<<d1<<endl;
    cout<<"两点间距离为"<<d2<<endl;
    return 0;
}
  • 写回答

3条回答 默认 最新

  • threenewbee 2020-03-25 17:27
    关注

    34行已经有Point p, p1;了
    40行又有Point p;
    导致重复定义。
    41行又定义了Point p(x, y);
    可以改名:
    40行
    Point p2;
    41行
    Point p3(x,y);
    下面调用相应修改

    #include <iostream>
    #include <math.h>
    using namespace std; 
    
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    class Point{
        public:
            Point(){
                x=0;
                y=0;
            }                  //无参数的构造函数 
            Point(float xx,float yy);  //有参数的构造函数 
            float calculate_distance(Point p1);
        private:
            float x,y;
    };
    Point::Point(float xx,float yy){
                x=xx;
                y=yy;
                cout<<"("<<x<<","<<y<<")"<<endl;
    }
    float Point::calculate_distance(Point p1){
                return (float)sqrt((x-p1.x)*(x-p1.x)+(y-p1.y)*(y-p1.y));
    
    }
    int main(int argc, char** argv) {
        Point p,p1;     //定义对象p、p1 
        float x,y;      //对象p的私有数据成员 
        float x1,y1;    //对象p1的私有对象成员
        float d1=0,d2=0;
        cin>>x>>y;
        cin>>x1>>y1;
        Point p2;         //调用无参数的构造函数 
        Point p3(x,y);    //调用有参数的构造函数 
        p1 = Point(x1,y1);
        d1=p.calculate_distance(p1);
        d2=p1.calculate_distance(p);
        cout<<"两点间距离为"<<d1<<endl;
        cout<<"两点间距离为"<<d2<<endl;
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用