首页 > 解决方案 > 我无法运行此代码来打印两点之间的距离。我只是一个初学者

问题描述

我编写了下面给出的代码来打印两点之间的距离,但它会引发很多错误。据我所知,错误在于“无效公式(距离o1,距离o2)”和主要。但是因为我只是一个初学者,所以我找不到确切的错误。请指导我。

class distance
{
     int a, b;
public:
    int c, d;

    friend void formula(distance, distance);

    distance(int a1, int b1)
    {
        a = a1;
        b = b1;
    }
 };

void formula(distance o1, distance o2)
{
    c = o2.a - o1.a;
    d = o2.b - o1.b;
    cout<<"The distance is "<<sqrt((c*c)+(d*d))<<endl;
}

int main()
{
    distance c1(1, 2), c2(3, 4);
    formula(c1, c2);
    return 0;
} 

标签: c++constructor

解决方案


在函数公式中,没有定义 c 和 d。如果你写,这个函数会知道它们

o1.c = ...
o1.d = ...

推荐阅读