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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章