odd error

I keep getting this pile of errors
I think it has something to do with using maps with a class I made, but I can't be sure. What do I do about it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_algobase.h:66:0,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/list:61,
                 from space-game.h:8,
                 from space-game-functions.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_pair.h: In function 'bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) [with _T1 = std::_List_iterator<SpaceShip>, _T2 = std::_List_iterator<Asteroid>]':
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_function.h:230:22:   instantiated from 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = std::pair<std::_List_iterator<SpaceShip>, std::_List_iterator<Asteroid> >]'
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_map.h:450:2:   instantiated from 'mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::pair<std::_List_iterator<SpaceShip>, std::_List_iterator<Asteroid> >, _Tp = double, _Compare = std::less<std::pair<std::_List_iterator<SpaceShip>, std::_List_iterator<Asteroid> > >, _Alloc = std::allocator<std::pair<const std::pair<std::_List_iterator<SpaceShip>, std::_List_iterator<Asteroid> >, double> >, mapped_type = double, key_type = std::pair<std::_List_iterator<SpaceShip>, std::_List_iterator<Asteroid> >]'
space-game-functions.cpp:293:49:   instantiated from here
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_pair.h:162:62: error: no match for 'operator<' in '__x->std::pair<std::_List_iterator<SpaceShip>, std::_List_iterator<Asteroid> >::second < __y->std::pair<std::_List_iterator<SpaceShip>, std::_List_iterator<Asteroid> >::second'
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_pair.h:162:62: error: no match for 'operator<' in '__y->std::pair<std::_List_iterator<SpaceShip>, std::_List_iterator<Asteroid> >::first < __x->std::pair<std::_List_iterator<SpaceShip>, std::_List_iterator<Asteroid> >::first'
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_pair.h:162:62: error: no match for 'operator<' in '__x->std::pair<std::_List_iterator<SpaceShip>, std::_List_iterator<Asteroid> >::first < __y->std::pair<std::_List_iterator<SpaceShip>, std::_List_iterator<Asteroid> >::first'
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_pair.h: In function 'bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) [with _T1 = std::_List_iterator<Asteroid>, _T2 = std::_List_iterator<Asteroid>]':
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_function.h:230:22:   instantiated from 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = std::pair<std::_List_iterator<Asteroid>, std::_List_iterator<Asteroid> >]'
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_map.h:450:2:   instantiated from 'mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::pair<std::_List_iterator<Asteroid>, std::_List_iterator<Asteroid> >, _Tp = double, _Compare = std::less<std::pair<std::_List_iterator<Asteroid>, std::_List_iterator<Asteroid> > >, _Alloc = std::allocator<std::pair<const std::pair<std::_List_iterator<Asteroid>, std::_List_iterator<Asteroid> >, double> >, mapped_type = double, key_type = std::pair<std::_List_iterator<Asteroid>, std::_List_iterator<Asteroid> >]'
space-game-functions.cpp:309:53:   instantiated from here
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_pair.h:162:62: error: no match for 'operator<' in '__x->std::pair<std::_List_iterator<Asteroid>, std::_List_iterator<Asteroid> >::second < __y->std::pair<std::_List_iterator<Asteroid>, std::_List_iterator<Asteroid> >::second'
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_pair.h:162:62: error: no match for 'operator<' in '__y->std::pair<std::_List_iterator<Asteroid>, std::_List_iterator<Asteroid> >::first < __x->std::pair<std::_List_iterator<Asteroid>, std::_List_iterator<Asteroid> >::first'
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_pair.h:162:62: error: no match for 'operator<' in '__x->std::pair<std::_List_iterator<Asteroid>, std::_List_iterator<Asteroid> >::first < __y->std::pair<std::_List_iterator<Asteroid>, std::_List_iterator<Asteroid> >::first'
mingw32-make: *** [space-game-functions.o] Error 1
mingw32-make: Target `space-game.exe' not remade because of errors.

Compilation exited abnormally with code 2 at Thu Jan 12 17:03:29 
It's often easier to look at the code than trying to read template errors. It could be that your key have no operator< defined.
If you want to use a custom class as the key, you need to provide a comparison function.
The compiler will asume bool operator<(const foo&, const foo&);
It's only two errors, not a pile.

You seem to have two maps defined:
map<pair<list<SpaceShip>::iterator, list<Asteroid>::iterator>, double>
and
map<pair<list<Asteroid>::iterator, list<Asteroid>::iterator>, double>

And the compiler tells you (in somewhat elaborate detail) that there's no way it can compare those iterator pairs to figure out which one is less than another.

The reason it is a bit more elaborate that one would expect is that operator< *is* defined for pairs: it is defined to compare the first elements of the pair and then the second elements of the pair. But there is no operator< for list iterators, and those are the final "no match for" error message.
is there any way to compare list iterators and make this work?
Certainly, if you can define the semantics of that comparison. What makes one list-of-asteroids iterator less than another?
It doesn't matter as long as it can be used in a map. Maybe address?

and how would I go about implementing it anyway?
return int(&obj ) < int(&obj2);
Last edited on
I suppose you could semi-meaningfully order by the address of the pointed-to list element, assuming you're never trying to store the end iterator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <utility>
#include <map>
#include <list>
struct Asteroid {};
typedef std::list<Asteroid> list_t;
typedef list_t::iterator iter_t;
typedef std::pair<iter_t, iter_t> pair_t;
struct Comp {
    bool operator()(const pair_t& p1, const pair_t& p2) const { 
        return &*p1.first < &*p2.first ? true : 
               &*p2.first < &*p1.first ? false : 
               &*p1.second < &*p2.second; // won't handle the end iterators!
    }
};
int main()
{
    list_t l;
    l.push_back(Asteroid());
    l.push_back(Asteroid());
    l.push_back(Asteroid());

    std::map<pair_t, double, Comp> m;
    m[make_pair(l.begin(), l.begin())] = 3.14;
    m[make_pair(l.begin(), std::next(l.begin()))] = 3.14;
    m[make_pair(l.begin(), l.begin())] = 3.14;

    std::cout << "Map has " << m.size() << " elements\n";
}

demo: http://ideone.com/oHpt5


Last edited on
to make this work with different objects in the list, do I need to use a template?
Topic archived. No new replies allowed.