views:

90

answers:

3

I have these two functions (with Point2D & LineVector (has 2 Point2D member variables) classes and SQUARE macro predefined)

inline float distance(const Point2D &p1,const Point2D &p2) {
    return sqrt(SQUARE(p2.getX()-p1.getX())+SQUARE(p2.getY()-p1.getY()));
}

inline float maxDistance(const LineVector &lv1,const LineVector &lv2) {
    return max(distance(lv1.p1,lv2.p2),distance(lv1.p2,lv2.p1));
}

but it gives compilation error in maxDistance() function (line 238) saying:

/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_iterator_base_types.h: In instantiation of `std::iterator_traits<Point2D>':
quadrilateral.cpp:238:   instantiated from here
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_iterator_base_types.h:129: error: no type named `iterator_category' in `class 
Point2D'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_iterator_base_types.h:130: error: no type named `value_type' in `class Point2D
'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_iterator_base_types.h:131: error: no type named `difference_type' in `class Point2D'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_iterator_base_types.h:132: error: no type named `pointer' in `class Point2D'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_iterator_base_types.h:133: error: no type named `reference' in `class Point2D'

Please suggest what is the error?

A: 

Point2D needs to inherit from std::iterator before it's going to place nicely with std::distance. std::distance needs some of the typedefs provided by the standard iterator type in order to use the correct method for measuring the distance.

EDIT: I'm assuming Point2D is supposed to be an iterator. If you're assuming std::distance is the distance formula, you are mistaken. std::distance returns the difference in position between two iterators pointing to a common container.

Billy ONeal
I did not understand anything. Please elaborate. The code looks perfectly fine to me then why this strange error?
avd
Okay you are saying that distance is already a function in standard library?
avd
Yes, `std::distance` is already part of the standard library: http://www.cplusplus.com/reference/std/iterator/distance/ That's why it's in namespace std, so you can define your own function called distance if you want. You don't have any `using namespace std` s around, do you? :)
Billy ONeal
@GMan: Ow! Get off my toes! (And maybe I'll get off yours)
Billy ONeal
@Billy: Fine. :P
GMan
@GMan: LOL! You didn't have to *delete* it. *Bill is cracking up*
Billy ONeal
No actually I have namespace std. I got it.
avd
@Billy ONeal: It doesn't matter whether the standard `distance` is in `std::` or not. C++ supports function overloading, based on argument types. If the code is indeed as the OP describes it, the call in the OP must resolve to the OP's `distance`, not to `std::distance`. `using namespace std` cannot affect that in any way.
AndreyT
@AndreyT: That's not what the OP is claiming. Honestly, when I wrote this answer, I paid more attention to the error message than the code (Which is evident in the first part of the answer :) ) I don't know.
Billy ONeal
A: 

Looks like you may be calling std::distance instead of your own distance function. There are a few reasons why this might be happening, but it's usually because of gratuitous use of

using namespace std;

Try explicitly using the namespace or class name when calling the distance function, or if it's in the global namespace

float result = ::distance(point1, point2);
tjohns20
Or (best option): Get rid of `using namespace std;`. It makes code in books look prettier, but there's a reason the standard library is in a namespace.
Billy ONeal
A: 

Maybe compiler interprets distance call as std::distance? Do you have using namespace std before this code? Try to rename the distance function and see what happens.

Alex Farber
I think it is obvious that the compiler interprets the call as `std::distance`. No doubt about that. Yet I don't see how it can interpret it that way given the above code.
AndreyT