map issue

Hello forum,

I have declared a map as follows:

map<Point3D, Photon> mPhotonList;

All of the above type is declared and defined. But i getting tons of error when i issue the following statement:

 
mPhotonList.insert(make_pair(point,photon));


The error is :

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
30
31
32
g++  -fopenmp -std=gnu++0x -O2 -g -o photontracer.o -c photontracer.cpp -I/usr/include/OpenEXR 
In file included from /usr/include/c++/4.6/string:50:0,
                 from /usr/include/c++/4.6/stdexcept:40,
                 from defines.h:13,
                 from photontracer.h:5,
                 from photontracer.cpp:1:
/usr/include/c++/4.6/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Point3D]’:
/usr/include/c++/4.6/bits/stl_tree.h:1277:4:   instantiated from ‘std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = std::pair<Point3D, PhotonTracer::Photon>, _Key = Point3D, _Val = std::pair<const Point3D, PhotonTracer::Photon>, _KeyOfValue = std::_Select1st<std::pair<const Point3D, PhotonTracer::Photon> >, _Compare = std::less<Point3D>, _Alloc = std::allocator<std::pair<const Point3D, PhotonTracer::Photon> >]’
/usr/include/c++/4.6/bits/stl_map.h:526:64:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::map<_Key, _Tp, _Compare, _Alloc>::value_type>::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(_Pair&&) [with _Pair = std::pair<Point3D, PhotonTracer::Photon>, <template-parameter-2-2> = void, _Key = Point3D, _Tp = PhotonTracer::Photon, _Compare = std::less<Point3D>, _Alloc = std::allocator<std::pair<const Point3D, PhotonTracer::Photon> >, typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::map<_Key, _Tp, _Compare, _Alloc>::value_type>::other>::iterator = std::_Rb_tree_iterator<std::pair<const Point3D, PhotonTracer::Photon> >]’
photontracer.cpp:137:58:   instantiated from here
/usr/include/c++/4.6/bits/stl_function.h:236:22: error: no match foroperator<’ in ‘__x < __y’
/usr/include/c++/4.6/bits/stl_function.h:236:22: note: candidates are:
/usr/include/c++/4.6/bits/stl_pair.h:207:5: note: template<class _T1, class _T2> constexpr bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:291:5: note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:341:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/stl_iterator.h:1049:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/stl_iterator.h:1055:5: note: template<class _Iterator> bool std::operator<(const std::move_iterator<_Iterator>&, const std::move_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/basic_string.h:2510:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2522:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/include/c++/4.6/bits/basic_string.h:2534:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/stl_vector.h:1290:5: note: template<class _Tp, class _Alloc> bool std::operator<(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
/usr/include/c++/4.6/bits/stl_tree.h:866:5: note: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator<(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_map.h:899:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_multimap.h:817:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&)
/usr/include/c++/4.6/tuple:586:5: note: template<class ... _TElements, class ... _UElements> bool std::operator<(const std::tuple<_TElements ...>&, const std::tuple<_Elements ...>&)
/usr/include/c++/4.6/bits/stl_deque.h:272:5: note: template<class _Tp, class _Ref, class _Ptr> bool std::operator<(const std::_Deque_iterator<_Tp, _Ref, _Ptr>&, const std::_Deque_iterator<_Tp, _Ref, _Ptr>&)
/usr/include/c++/4.6/bits/stl_deque.h:280:5: note: template<class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR> bool std::operator<(const std::_Deque_iterator<_Tp, _RefL, _PtrL>&, const std::_Deque_iterator<_Tp, _RefR, _PtrR>&)
/usr/include/c++/4.6/bits/stl_deque.h:1935:5: note: template<class _Tp, class _Alloc> bool std::operator<(const std::deque<_Tp, _Alloc>&, const std::deque<_Tp, _Alloc>&)
/usr/include/c++/4.6/bits/stl_stack.h:259:5: note: template<class _Tp, class _Seq> bool std::operator<(const std::stack<_Tp, _Seq>&, const std::stack<_Tp, _Seq>&)
/usr/include/c++/4.6/bits/stl_list.h:1593:5: note: template<class _Tp, class _Alloc> bool std::operator<(const std::list<_Tp, _Alloc>&, const std::list<_Tp, _Alloc>&)
make: *** [photontracer.o] Error 1



Any idea folks ?


Thanks
Sajjad
I guess you didn't implement operator < (less than) in class Point3D.

There is no meaning for me to have the <(less than) operator in my case . But i did it now any way as follows:

 
 bool operator<(const Point3D &p) { return (x < p.x && y < p.y && z < p.z); }


I am still getting some error but a little less than before, thanks for the hint.

1
2
3
4
5
6
7
8
9
10
11
12
13
g++  -fopenmp -std=gnu++0x -O2 -g -o photontracer.o -c photontracer.cpp -I/usr/include/OpenEXR 
In file included from /usr/include/c++/4.6/string:50:0,
                 from /usr/include/c++/4.6/stdexcept:40,
                 from defines.h:13,
                 from photontracer.h:5,
                 from photontracer.cpp:1:
/usr/include/c++/4.6/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Point3D]’:
/usr/include/c++/4.6/bits/stl_tree.h:1277:4:   instantiated from ‘std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = std::pair<Point3D, PhotonTracer::Photon>, _Key = Point3D, _Val = std::pair<const Point3D, PhotonTracer::Photon>, _KeyOfValue = std::_Select1st<std::pair<const Point3D, PhotonTracer::Photon> >, _Compare = std::less<Point3D>, _Alloc = std::allocator<std::pair<const Point3D, PhotonTracer::Photon> >]’
/usr/include/c++/4.6/bits/stl_map.h:526:64:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::map<_Key, _Tp, _Compare, _Alloc>::value_type>::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(_Pair&&) [with _Pair = std::pair<Point3D, PhotonTracer::Photon>, <template-parameter-2-2> = void, _Key = Point3D, _Tp = PhotonTracer::Photon, _Compare = std::less<Point3D>, _Alloc = std::allocator<std::pair<const Point3D, PhotonTracer::Photon> >, typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::map<_Key, _Tp, _Compare, _Alloc>::value_type>::other>::iterator = std::_Rb_tree_iterator<std::pair<const Point3D, PhotonTracer::Photon> >]’
photontracer.cpp:137:58:   instantiated from here
/usr/include/c++/4.6/bits/stl_function.h:236:22: error: passing ‘const Point3D’ as ‘this’ argument of ‘bool Point3D::operator<(const Point3D&)’ discards qualifiers [-fpermissive]
make: *** [photontracer.o] Error 1



I believe i need more hint


Thanks
Sajjad
For maps, you're required to have the < operator overloading for proper ordering of key-value pairs. From here: http://cplusplus.com/reference/map/map/

CPlusPlus wrote:
Elements follow a strict weak ordering at all times. Unordered associative arrays, like unordered_map, are available in implementations following TR1.


Your last error looks like it comes from the way you implemented your comparison operator. Try adding const at the end of the function declaration and at the end of the function definition header, but before the definition block. Read here for a little more information: http://stackoverflow.com/questions/9776533/error-passing-const-xxx-as-this-argument-of-xxx-discards-qualifiers
Topic archived. No new replies allowed.