maps, iterators, and complex structs - STL errors

Posted by Austin Hyde on Stack Overflow See other posts from Stack Overflow or by Austin Hyde
Published on 2010-03-29T03:24:22Z Indexed on 2010/03/29 3:43 UTC
Read the original article Hit count: 478

Filed under:
|
|
|

So, I have two structs:

struct coordinate {
    float x;
    float y;
}

struct person {
    int id;
    coordinate location;
}

and a function operating on coordinates:

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

In my main method, I have the following code:

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;
    }
}

However, I get the following error upon build:

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’

And it blames it on the line:

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

Why does the STL complain about my code?

© Stack Overflow or respective owner

Related posts about c++

Related posts about stl