2010-03-29 86 views
2

所以,我有兩個結構:地圖,迭代器和複雜的結構 - STL錯誤

struct coordinate { 
    float x; 
    float y; 
} 

struct person { 
    int id; 
    coordinate location; 
} 

和功能上的座標操作:

float distance(const coordinate& c1, const coordinate& c2); 

在我的主要方法,我有以下代碼:

map<int,person> people; 
// populate people 
map<int,map<float,int> > distance_map; 
map<int,person>::iterator it1,it2; 
for (it1=people.begin(); it1!=people.end(); ++it1) { 
    for (it2=people.begin(); it2!=people.end(); ++it2) { 
     float d = distance(it1->second.location,it2->second.location); 
     distance_map[it1->first][d] = it2->first; 
    } 
} 

但是,我在編譯出現以下錯誤:

stl_iterator_base_types.h: In instantiation of ‘std::iterator_traits<coordinate>’: 
stl_iterator_base_types.h:129: error: no type named ‘iterator_category’ in ‘struct coordinate’ 
stl_iterator_base_types.h:130: error: no type named ‘value_type’ in ‘struct coordinate’ 
stl_iterator_base_types.h:131: error: no type named ‘difference_type’ in ‘struct coordinate’ 
stl_iterator_base_types.h:132: error: no type named ‘pointer’ in ‘struct coordinate’ 
stl_iterator_base_types.h:133: error: no type named ‘reference’ in ‘struct coordinate’ 

它指責就行了:

float d = distance(it1->second.location,it2->second.location); 

爲什麼在STL抱怨我的代碼?

+1

可能無關緊要的問題:浮動工具是否會爲關鍵值工作? – egrunin 2010-03-29 03:43:28

回答

6

標準庫中有一個名爲std::distance的函數,它在迭代器上運行。所以它看起來像編譯器試圖調用那個而不是你的。如果你使用它我會刪除using namespace std;指令,只是說using std::map;

2

因爲你可能有一個using namespace std;地方編譯器混淆你的函數float distance(const coordinate& c1, const coordinate& c2)algorithm頭的功能。

template <class InputIterator> 
inline |stl-iterator_traits|<InputIterator>::difference_type 
distance(InputIterator first, InputIterator last); 

template <class |stl-InputIterator|, class Distance> 
void distance(InputIterator first, InputIterator last, Distance& n);