映射,迭代器和复杂结构 - STL错误

Aus*_*yde 2 c++ debugging iterator stl

所以,我有两个结构:

struct coordinate {
    float x;
    float y;
}

struct person {
    int id;
    coordinate location;
}
Run Code Online (Sandbox Code Playgroud)

以及在坐标上操作的函数:

float distance(const coordinate& c1, const coordinate& c2);
Run Code Online (Sandbox Code Playgroud)

在我的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我在构建时收到以下错误:

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’
Run Code Online (Sandbox Code Playgroud)

它归咎于它:

float d = distance(it1->second.location,it2->second.location);
Run Code Online (Sandbox Code Playgroud)

为什么STL会抱怨我的代码?

Der*_*ter 6

标准库中有一个函数std::distance,它在迭代器上运行.所以看起来编译器试图调用那个而不是你的.using namespace std;如果您正在使用它,我会删除该指令,并且只是说using std::map;等.