又一個非常非常詭異的一個編譯錯誤。當我第一次遇到這個錯誤的時候頭都暈了。還是先把代碼貼上來吧:
- #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;
- }
#include <iostream>
問題出在哪裏呢?我們只是寫了一段普通的代碼,卻爆出來一大堆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'