error:no type named iterator_category in struct

又一个非常非常诡异的一个编译错误。当我第一次遇到这个错误的时候头都晕了。还是先把代码贴上来吧:

  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include <math.h>  
  4. using namespace std;  
  5.   
  6. typedef struct TagPoint  
  7. {  
  8.     int x, y;  
  9. }Point;  
  10.   
  11. inline double distance(const Point& a, const Point& b)  
  12. {  
  13.     return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));  
  14. }  
  15.   
  16. int main(int argc, char **argv)  
  17. {  
  18.     Point a, b;  
  19.     printf(”%lf\n”, distance(a, b));  
  20.     return 0;  
  21. }  
#include <iostream>




#include <stdio.h> #include <math.h> using namespace std; typedef struct TagPoint { int x, y; }Point; inline double distance(const Point& a, const Point& b) { return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y)); } int main(int argc, char **argv) { Point a, b; printf("%lf\n", distance(a, b)); return 0; }

问题出在哪里呢?我们只是写了一段普通的代码,却爆出来一大堆STL相关的错误信息(STL的错误信息实在是太难看了)。后来仔细研究发现,是函数命名存在问题。猛然想起distance是STL中求迭代器距离的一个函数,这里冲突了。按理来说编译器应该可以推断出使用哪个函数吧~~也曾经遇到过类似的编译错误,有关transform函数的

好啦,解决就是给distance换一个名字。附上编译错误信息:

C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_i
terator_base_types.h: In instantiation of `std::iterator_traits':
test.cpp:19:   instantiated from here
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_i
terator_base_types.h:129: error: no type named `iterator_category' in `struct Ta
gPoint'
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_i
terator_base_types.h:130: error: no type named `value_type' in `struct TagPoint'

C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_i
terator_base_types.h:131: error: no type named `difference_type' in `struct TagP
oint'
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_i
terator_base_types.h:132: error: no type named `pointer' in `struct TagPoint'
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_i
terator_base_types.h:133: error: no type named `reference' in `struct TagPoint'
发布了48 篇原创文章 · 获赞 17 · 访问量 2万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章