Итак, я скомпилировал ваш код в Visual Studio 2019 и пару онлайн-компиляторов, и он прекрасно работает.
Я предположил несколько вещей, но, честно говоря, добавление ::
ни на что не должно повлиять.
также, я не мог найти, где вы объявили float d
, было бы действительно все выяснить, если бы вы показали нам сообщение об ошибке!
#include <iostream>
#include <cmath>
using namespace std;
class Point {
float x=0.f, y=0.f, z=0.f;
public:
Point() {}
Point(float x1, float y1, float z1) :x(x1), y(y1), z(z1) {}
float getX() const{ return x; }
float getY() const{ return y; }
float getZ() const{ return z; }
};//Class object with x, y, and z variable and has functions to return values
float distance(Point p1, Point p2);
int main() {
Point P1, P2;
float d = distance(P1, P2); // throws an error but just adding -> ::distance(P1, P2) works fine! why?
cout << "Distance between P1 and P2: " << d << endl;
return 0;
}
float distance(Point p1, Point p2) {
float d;
int x0 = p1.getX(), y0 = p1.getY(), z0 = p1.getZ();
int x1 = p2.getX(), y1 = p2.getY(), z1 = p2.getZ();
d = sqrt(pow((x1 - x0), 2) + pow((y1 - y0), 2) + pow((z1 - z0), 2));
return d;
}