您的位置:首页 > 编程语言 > C语言/C++

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

2016-07-28 15:58 651 查看
#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;}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息