新手求助。。望高人指点。。

vgfcjv 2010-04-23 01:05:52
用类求两点之间的距离,为什么运算结果就是不正确呢。。?每次返回的都是同一个很奇怪的值 而且还是负的。。。。。

#include<iostream.h>
#include<math.h>
class point
{
private:
double x,y;
public:
point(double xx,double yy)
{cout<<"使用点构造函数"<<endl;
x=xx;y=yy=y;
}
point(point &t)
{cout<<"使用点拷贝构造函数"<<endl;
x=t.x;y=t.y;
}
double getx(){return x;}
double gety(){return y;}
};

class distance
{
private:
point p1,p2;
double disc;
public:
distance(point t1,point t2);
double getdisc(){return disc;}

};
distance::distance(point t1,point t2):p1(t1),p2(t2)
{
cout<<"使用距离构造函数"<<endl;
double x=double(t1.getx()-t2.getx());
double y=double(t1.gety()-t2.gety());
double disc=sqrt(x*x+y*y);
}

void main(void)
{
double a,b,c,d;
cout<<"please enter the x coordinate and the y coordinate:"<<endl;
cin>>a>>b;
cout<<"please enter the x coordinate and the y coordinate:"<<endl;
cin>>c>>d;
point myp1(a,b),myp2(c,d);
distance myd(myp1,myp2);
cout<<"the distance is:"<<endl;
cout<<myd.getdisc()<<endl;
}
...全文
187 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
wade_2003 2010-04-24
  • 打赏
  • 举报
回复

#include<iostream>
#include<math.h>

using namespace std;
class point
{
private:
double x,y;
public:
point(double xx,double yy)
{
cout<<"使用点构造函数"<<endl;
x=xx;y=yy;
}
point(point &t)
{
cout<<"使用点拷贝构造函数"<<endl;
x=t.x;y=t.y;
}
double getx(){return x;}
double gety(){return y;}
};

class distances
{
private:
point p1,p2;
double disc;
public:
distances(point t1,point t2);
double getdisc(){return disc;}

};
distances::distances(point t1,point t2):p1(t1),p2(t2)
{
cout<<"使用距离构造函数"<<endl;
double x=double(t1.getx()-t2.getx());
double y=double(t1.gety()-t2.gety());
disc=sqrt(x*x+y*y);
}

int main(void)
{
double a,b,c,d;
cout<<"please enter the x coordinate and the y coordinate:"<<endl;
cin>>a>>b;
cout<<"please enter the x coordinate and the y coordinate:"<<endl;
cin>>c>>d;
point myp1(a,b),myp2(c,d);
distances myd(myp1,myp2);
cout<<"the distance is:"<<endl;
cout<<myd.getdisc()<<endl;
return 0;
}

ForestDB 2010-04-23
  • 打赏
  • 举报
回复
帮顶。
liutengfeigo 2010-04-23
  • 打赏
  • 举报
回复
接点分
vgfcjv 2010-04-23
  • 打赏
  • 举报
回复
谢谢~~~~解决了·~~~·[Quote=引用 6 楼 xianglitian 的回复:]
引用 3 楼 jiuchang 的回复:
distance::distance(point t1,point t2):p1(t1),p2(t2)
{
cout<<"使用距离构造函数"<<endl;
double x=double(t1.getx()-t2.getx());
double y=double(t1.gety()-t2.gety());
double disc=sqrt(x*……
[/Quote]
vgfcjv 2010-04-23
  • 打赏
  • 举报
回复
明白了!谢谢~~~~[Quote=引用 8 楼 jiuchang 的回复:]
用double就变成了声明变量,意思是这个地方的disc是一个临时变量,不是类里定义的哪个,这个值是不是会被赋给类里的disc的
要看下变量 的作用范围
[/Quote]
vgfcjv 2010-04-23
  • 打赏
  • 举报
回复
using namespace std;这是什么作用的啊?
而且还报错-___-|error C2872: 'distance' : ambiguous symbol

[Quote=引用 5 楼 wade_2003 的回复:]
C/C++ code

#include<iostream>
#include<math.h>
using namespace std;
class point
{
private:
double x,y;
public:
point(double xx,double yy)
{
cout<<"使用点构造函数"<<endl;
……
[/Quote]
jiuchang 2010-04-23
  • 打赏
  • 举报
回复
用double就变成了声明变量,意思是这个地方的disc是一个临时变量,不是类里定义的哪个,这个值是不是会被赋给类里的disc的
要看下变量 的作用范围
vgfcjv 2010-04-23
  • 打赏
  • 举报
回复
真的诶。。。
但不是把sqrt(x*x+y*y)的值赋给disc了么 怎么double又会被随机赋值了呢?刚学。。不太懂。。[Quote=引用 3 楼 jiuchang 的回复:]
distance::distance(point t1,point t2):p1(t1),p2(t2)
{
cout<<"使用距离构造函数"<<endl;
double x=double(t1.getx()-t2.getx());
double y=double(t1.gety()-t2.gety());
double disc=sqrt(x*x+y*y);
}

这儿也错了,最后……
[/Quote]
向立天 2010-04-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jiuchang 的回复:]
distance::distance(point t1,point t2):p1(t1),p2(t2)
{
cout<<"使用距离构造函数"<<endl;
double x=double(t1.getx()-t2.getx());
double y=double(t1.gety()-t2.gety());
double disc=sqrt(x*x+y*y);
}

这儿也错了,最后……
[/Quote]

没错
这里你用局部变量把成员变量覆盖了
wade_2003 2010-04-23
  • 打赏
  • 举报
回复

#include<iostream>
#include<math.h>
using namespace std;
class point
{
private:
double x,y;
public:
point(double xx,double yy)
{
cout<<"使用点构造函数"<<endl;
x=xx;y=yy;
}
point(point &t)
{
cout<<"使用点拷贝构造函数"<<endl;
x=t.x;y=t.y;
}
double getx(){return x;}
double gety(){return y;}
};

class distance
{
private:
point p1,p2;
double disc;
public:
distance(point t1,point t2);
double getdisc(){return disc;}

};
distance::distance(point t1,point t2):p1(t1),p2(t2)
{
cout<<"使用距离构造函数"<<endl;
double x=double(t1.getx()-t2.getx());
double y=double(t1.gety()-t2.gety());
disc=sqrt(x*x+y*y);
}

int main(void)
{
double a,b,c,d;
cout<<"please enter the x coordinate and the y coordinate:"<<endl;
cin>>a>>b;
cout<<"please enter the x coordinate and the y coordinate:"<<endl;
cin>>c>>d;
point myp1(a,b),myp2(c,d);
distance myd(myp1,myp2);
cout<<"the distance is:"<<endl;
cout<<myd.getdisc()<<endl;
return 0;
}

vgfcjv 2010-04-23
  • 打赏
  • 举报
回复
额。。。怎么当时没报错。。
可是,我刚刚改过来,试了下,结果还是不对挖。。。[Quote=引用 1 楼 jiuchang 的回复:]
point(double xx,double yy)
{cout<<"使用点构造函数"<<endl;
x=xx;y=yy=y;
}

这儿错了,多了一个=y
[/Quote]
jiuchang 2010-04-23
  • 打赏
  • 举报
回复
distance::distance(point t1,point t2):p1(t1),p2(t2)
{
cout<<"使用距离构造函数"<<endl;
double x=double(t1.getx()-t2.getx());
double y=double(t1.gety()-t2.gety());
double disc=sqrt(x*x+y*y);
}

这儿也错了,最后多了个double,把disc变成了临时变量,就是类里定义的距离了,而变成了一个随机数,去掉double后应该就对了
向立天 2010-04-23
  • 打赏
  • 举报
回复
point(double xx,double yy)
{cout<<"使用点构造函数"<<endl;
x=xx;y=yy=y;
}

你这里写错了吧
jiuchang 2010-04-23
  • 打赏
  • 举报
回复
point(double xx,double yy)
{cout<<"使用点构造函数"<<endl;
x=xx;y=yy=y;
}

这儿错了,多了一个=y

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧