鱼C论坛

 找回密码
 立即注册
查看: 2395|回复: 3

[已解决]这个程序到底哪里出错了啊???

[复制链接]
发表于 2018-1-26 14:13:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<iostream>
#include<math.h>
using namespace std;
class point;
class distance
{
private:
    double length;
public:
    distance(double h);
    void qiuhe(point a,point b);
};
class point
{
private:
    double x;
    double y;
public:
    friend class distance;
    point(double a,double b);
};
int main()
{
    point x1(0,0),x2(0,0);
    distance s(1.0);
    s.qiuhe(x1,x2);
    return 0;
}
point::point(double a,double b)
{
    cin>>a>>b;
    x=a;y=b;
}
distance::distance(double h)
{
    length=h;
    cin>>h;
    length=h;
}
void distance::qiuhe(point a,point b)
{
    distance temp(0.0);
    temp=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    cout<<temp.length;
}

最佳答案
2018-1-26 17:55:45
using namespace std;
偷懒可不是什么好事

碰巧std命名空间也有一个叫distance这玩意的

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

  3. //using namespace std;
  4. class point;

  5. class distance
  6. {
  7. private:
  8.         double length;
  9. public:
  10.         distance(double h);
  11.         void qiuhe(point a, point b);
  12. };

  13. class point
  14. {
  15. private:
  16.         double x;
  17.         double y;
  18. public:
  19.         friend class distance;
  20.         point(double a, double b);
  21. };

  22. int main()
  23. {
  24.         point x1(0, 0), x2(0, 0);
  25.         distance s(1.0);
  26.         s.qiuhe(x1, x2);
  27.         return 0;
  28. }
  29. point::point(double a, double b)
  30. {
  31.         std::cin >> a >> b;
  32.         x = a; y = b;
  33. }
  34. distance::distance(double h)
  35. {
  36.         length = h;
  37.         std::cin >> h;
  38.         length = h;
  39. }
  40. void distance::qiuhe(point a, point b)
  41. {
  42.         distance temp(0.0);
  43.         temp = sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
  44.         std::cout << temp.length;
  45. }
复制代码
无标题.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-1-26 14:41:15 | 显示全部楼层
感觉好乱,你要习惯类用大写字母开头,同类的函数实现放到一起
还有类方法qiuhe中定义类对象是什么鬼
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-26 17:55:45 | 显示全部楼层    本楼为最佳答案   
using namespace std;
偷懒可不是什么好事

碰巧std命名空间也有一个叫distance这玩意的

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

  3. //using namespace std;
  4. class point;

  5. class distance
  6. {
  7. private:
  8.         double length;
  9. public:
  10.         distance(double h);
  11.         void qiuhe(point a, point b);
  12. };

  13. class point
  14. {
  15. private:
  16.         double x;
  17.         double y;
  18. public:
  19.         friend class distance;
  20.         point(double a, double b);
  21. };

  22. int main()
  23. {
  24.         point x1(0, 0), x2(0, 0);
  25.         distance s(1.0);
  26.         s.qiuhe(x1, x2);
  27.         return 0;
  28. }
  29. point::point(double a, double b)
  30. {
  31.         std::cin >> a >> b;
  32.         x = a; y = b;
  33. }
  34. distance::distance(double h)
  35. {
  36.         length = h;
  37.         std::cin >> h;
  38.         length = h;
  39. }
  40. void distance::qiuhe(point a, point b)
  41. {
  42.         distance temp(0.0);
  43.         temp = sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
  44.         std::cout << temp.length;
  45. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-1-27 09:56:30 | 显示全部楼层
人造人 发表于 2018-1-26 17:55
using namespace std;
偷懒可不是什么好事

谢谢,谢谢!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-5 23:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表