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

#include <iostream>
#include <stdio.h>
#include <math.h>
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("<<a.getX()<<","<<a.getY()<<")"<<endl;
	cout<<"distance of a to b ab="<<distance(a, b)<<endl;
	cout<<"distance of a to c ac="<<distance(b, c)<<endl;
	return 0;
}

在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<Point>’:
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’
</span>

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


#include <iostream>
#include <stdio.h>
#include <math.h>
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("<<a.getX()<<","<<a.getY()<<")"<<endl;
	cout<<"distance of a to b ab="<<Distance(a, b)<<endl;
	cout<<"distance of a to c ac="<<Distance(b, c)<<endl;
	return 0;
}


 

                
  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值