所以,我有两个结构:
struct coordinate {
float x;
float y;
}
struct person {
int id;
coordinate location;
}
和在坐标上运行的函数:
float distance(const coordinate& c1, const coordinate& c2);
在我的main方法中,我有以下代码:
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会抱怨我的代码?
答案 0 :(得分:6)
标准库中有一个名为std::distance
的函数,它在迭代器上运行。所以看起来编译器试图调用那个而不是你的。如果你使用它,我会删除using namespace std;
指令,只需说using std::map;
等。
答案 1 :(得分: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);