I have these two functions (with Point2D & LineVector (has 2 Point2D member variables) classes and SQUARE macro predefined)
inline float distance(const Poi开发者_开发问答nt2D &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?
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);
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.
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.
精彩评论