0

So I have this program that has a 3D point class and a function that finds out the distance between two points. When I use the distance function normally in the main, it gives an error. But when I add scope resolution operator, the code works. What is causing the error and how the scope resolution operator is fixing it?

This error occurs in Dev-C++ and Codeblocks but works fine with Atom IDE using gpp-compiler plugin with MinGW compiler.

#include <iostream>
#include <cmath>
using namespace std;

class Point{...} //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;
    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;
}

crestniraz
  • 66
  • 6
  • 4
    please include the error message in the question – 463035818_is_not_an_ai May 14 '19 at 09:42
  • 1
    Possible duplicate: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – 463035818_is_not_an_ai May 14 '19 at 09:43
  • 1
    Short answer: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – lisyarus May 14 '19 at 09:44
  • By the way, you did not initialize ```P1``` and ```P2``` in your main. – Croppi May 14 '19 at 09:44
  • Might this be a question of namespace? There might be a `std::distance` function. –  May 14 '19 at 09:44
  • @Croppi more is missing from this snippet. @ OP please include a [mcve] and the error message – 463035818_is_not_an_ai May 14 '19 at 09:45
  • @generic_opto_guy [there is](https://en.cppreference.com/w/cpp/iterator/distance) – Mike van Dyke May 14 '19 at 09:45
  • Maybe a compiler bug. Which version of Dev-C++, although they are all pretty old at this point? On Windows Visual Studio is free, or you can make sure you have a recent version of MinGW/GCC directly for a linux-like environment. While there is an `std::distance`, and you used `std`, I do not believe it should cause a problem with you declaring `distance` that way (quickly checked VS2019 and GCC 5.4). – Fire Lancer May 14 '19 at 09:48
  • @FireLancer given the absence of a mcve its a bit far-fetched to speculate a compiler error imho. I mean we dont even know what error is reported – 463035818_is_not_an_ai May 14 '19 at 09:50
  • @formerlyknownas_463035818 We have the code though, and that code should work fine, and does on any recent GCC or VS I tried (given a dummy `Point` etc. needed for `::distance` as well), even if I added `` intentionally. `std::distance` is a template function, and he declared `float distance(Point p1, Point p2)` before he tried to use it. – Fire Lancer May 14 '19 at 09:51
  • 2
    @FireLancer we have code but it is incomplete and sprinkled with syntax errors, and we have no information on what the error actually says.... – 463035818_is_not_an_ai May 14 '19 at 09:54

2 Answers2

1

It's impossible to tell for sure without the actual error message, but it looks like the problem is using namespace std;. This brings in the function std::distance() which isn't what you want, but using the scope operator to request the global distance() function works again.

Avoid bringing in the entire std namespace.

Harun
  • 1,582
  • 1
  • 10
  • 8
  • 2
    It shouldn't try to use `std::distance` in the code as posted, even if `` is included. `float distance(Point p1, Point p2)` should be preferred over the template version. – Fire Lancer May 14 '19 at 09:50
  • std::distance is defined in and not – Shriram May 14 '19 at 09:54
  • 3
    @Shriram There is no reason why couldn't include , standard does not disallow including additional files – VLL May 14 '19 at 09:58
1

So I compiled your code in Visual Studio 2019 and a couple of online compilers and it works just fine. I assumed a few things but honestly adding a :: shouldn't affect anything in this case.

also , I couldn't find where you declared float d, It would really clear things up if you showed us the error message!

#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;
}
Shriram
  • 167
  • 1
  • 13