c++ - 我无法运行此代码来打印两点之间的距离。我只是一个初学者
问题描述
我编写了下面给出的代码来打印两点之间的距离,但它会引发很多错误。据我所知,错误在于“无效公式(距离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 和 d。如果你写,这个函数会知道它们
o1.c = ...
o1.d = ...
推荐阅读
- c# - 在哪里可以找到 System.Memory 文档?
- python - 将对象保存在内存中并避免重新加载泡菜
- javascript - mp3 文件的 Google Picker ViewId
- python - 无法在 Windows 10 上的 python 3.6 64 位上运行 pyqt5(没有 anaconda 没有 virtualenv)
- sql - T-SQL 存储过程中带有 CTE 的 SELECT/UPDATE 是原子的吗?
- javascript - 如何在特定程度上获取Javascript中椭圆的边缘坐标?
- javascript - PHP单击链接而不是行切换
- c# - 4.10 带有 NuGet 包参考的插件无法正确部署
- git - 掌舵点存储库到 monorepo 包?
- angular - 带有 Jest 单元测试的 Angular 6