C++编译错误no type named ‘iterator_category’ in ‘class Point’,distance重名

#include 
#include 
#include 
using namespace std;

class Point{
private:
	float x;
	float y;
public:
	float getX();
	float getY();
	Point(float a = 0.0f, float b = 0.0f):x(a),y(b){};
	friend float distance(Point & left, Point & right);
};
float Point::getX(){
	return x;
}
float Point::getY(){
	return y;
}
float distance(Point & left, Point & right){
	return sqrt((left.x - right.x)*(left.x - right.x) + 
		(left.y - right.y)*(left.x - right.y));
}
int main()
{
	Point a,b(1,1),c(2,1);
	cout<<"a("<
在linux编译出现如下错误信息:
g++ -o distance distanceOfTwoPoint.cpp 
In file included from /usr/include/c++/4.6/bits/stl_algobase.h:66:0,
                 from /usr/include/c++/4.6/bits/char_traits.h:41,
                 from /usr/include/c++/4.6/ios:41,
                 from /usr/include/c++/4.6/ostream:40,
                 from /usr/include/c++/4.6/iostream:40,
                 from distanceOfTwoPoint.cpp:1:
/usr/include/c++/4.6/bits/stl_iterator_base_types.h: In instantiation of ‘std::iterator_traits’:
distanceOfTwoPoint.cpp:29:47:   instantiated from here
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:166:53: error: no type named ‘iterator_category’ in ‘class Point’
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:167:53: error: no type named ‘value_type’ in ‘class Point’
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:168:53: error: no type named ‘difference_type’ in ‘class Point’
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:169:53: error: no type named ‘pointer’ in ‘class Point’
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:170:53: error: no type named ‘reference’ in ‘class Point’

后来网上查找,知道原来distance与STL的迭代器求距离函数重名,改掉名字便可,如下:


#include 
#include 
#include 
using namespace std;

class Point{
private:
	float x;
	float y;
public:
	float getX();
	float getY();
	Point(float a = 0.0f, float b = 0.0f):x(a),y(b){};
	friend float Distance(Point & left, Point & right);
};
float Point::getX(){
	return x;
}
float Point::getY(){
	return y;
}
float Distance(Point & left, Point & right){
	return sqrt((left.x - right.x)*(left.x - right.x) + 
		(left.y - right.y)*(left.x - right.y));
}
int main()
{
	Point a,b(1,1),c(2,1);
	cout<<"a("<

你可能感兴趣的:(C++编程_编译错误)