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;}
相关文章推荐
- C++中cin、getline()
- 数独问题
- 用sublime编译C++的方法
- 用sublime编译C++的方法
- C\C++中strcat()函数
- NYOJzb的生日
- C++ typedef用法小结
- 5-43 字符串关键字的散列映射
- 【C++学习之路】解决:无法查找或打开 PDB 文件
- c语言和c++的输入,输出和异常
- C++Operator
- 【C++学习之路】VS2015中cout、cin未声明的标识符错误的处理
- C# 调用C++ SDK/API的问题总结
- c++中的继承的讲解
- C++ map用法
- 5-42 整型关键字的散列映射
- 继承下的几个概念的区别
- c++List用法
- 基于C++浮点数(float、double)类型数据比较与转换的详解
- windows7下makefile例程