views:

88

answers:

2

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?

+1  A: 

There's a function in the standard library called std::distance, which operates on iterators. So it looks like the compiler is trying to call that one instead of yours. I'd remove the using namespace std; directive if you're using it, and just say using std::map;, etc.

Derek Ledbetter
+1  A: 

Since you probably have a using namespace std; somewhere the compiler confuses your function float distance(const coordinate& c1, const coordinate& c2) with the functions from the algorithm header.

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);
honk