Home > Net >  C Map with key and value data type as uint32_t gives error
C Map with key and value data type as uint32_t gives error

Time:10-17

On using uint32_t data type for the key and value in a map. I am getting an error no known conversion for argument.

Here is what I tried

int main() {
    uint32_t a = 5;
    uint32_t b = 9;
    map <uint32_t, uint32_t> Mapper;
    Mapper.insert({9,8});
    auto imageFoundIt = Mapper.find(make_pair(a, b));
    
    return 0;
}

Here is the error stack trace

g   /tmp/cpzeeEDm4C.cpp
/tmp/cpzeeEDm4C.cpp: In function 'int main()':
/tmp/cpzeeEDm4C.cpp:10:52: error: no matching function for call to 'std::map<unsigned int, unsigned int>::find(std::pair<unsigned int, unsigned int>)'
   10 |     auto imageFoundIt = Mapper.find(make_pair(a, b));
      |                                                    ^
In file included from /usr/include/c  /9/map:61,
                 from /usr/include/x86_64-linux-gnu/c  /9/bits/stdc  .h:81,
                 from /tmp/cpzeeEDm4C.cpp:3:
/usr/include/c  /9/bits/stl_map.h:1168:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::find(const key_type&) [with _Key = unsigned int; _Tp = unsigned int; _Compare = std::less<unsigned int>; _Alloc = std::allocator<std::pair<const unsigned int, unsigned int> >; std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const unsigned int, unsigned int> >; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = unsigned int]'
 1168 |       find(const key_type& __x)
      |       ^~~~
/usr/include/c  /9/bits/stl_map.h:1168:28: note:   no known conversion for argument 1 from 'std::pair<unsigned int, unsigned int>' to 'const key_type&' {aka 'const unsigned int&'}
 1168 |       find(const key_type& __x)
      |            ~~~~~~~~~~~~~~~~^~~
/usr/include/c  /9/bits/stl_map.h:1174:2: note: candidate: 'template<class _Kt> decltype (((std::map<_Key, _Tp, _Compare, _Alloc>*)this)->std::map<_Key, _Tp, _Compare, _Alloc>::_M_t._M_find_tr(__x)) std::map<_Key, _Tp, _Compare, _Alloc>::find(const _Kt&) [with _Kt = _Kt; _Key = unsigned int; _Tp = unsigned int; _Compare = std::less<unsigned int>; _Alloc = std::allocator<std::pair<const unsigned int, unsigned int> >]'
 1174 |  find(const _Kt& __x) -> decltype(_M_t._M_find_tr(__x))
      |  ^~~~
/usr/include/c  /9/bits/stl_map.h:1174:2: note:   template argument deduction/substitution failed:
/usr/include/c  /9/bits/stl_map.h: In substitution of 'template<class _Kt> decltype (((std::map<unsigned int, unsigned int>*)this)->std::map<unsigned int, unsigned int>::_M_t.std::_Rb_tree<unsigned int, std::pair<const unsigned int, unsigned int>, std::_Select1st<std::pair<const unsigned int, unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, unsigned int> > >::_M_find_tr(__x)) std::map<unsigned int, unsigned int>::find<_Kt>(const _Kt&) [with _Kt = std::pair<unsigned int, unsigned int>]':
/tmp/cpzeeEDm4C.cpp:10:52:   required from here
/usr/include/c  /9/bits/stl_map.h:1174:2: error: no matching function for call to 'std::_Rb_tree<unsigned int, std::pair<const unsigned int, unsigned int>, std::_Select1st<std::pair<const unsigned int, unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, unsigned int> > >::_M_find_tr(const std::pair<unsigned int, unsigned int>&)'
In file included from /usr/include/c  /9/map:60,
                 from /usr/include/x86_64-linux-gnu/c  /9/bits/stdc  .h:81,
                 from /tmp/cpzeeEDm4C.cpp:3:
/usr/include/c  /9/bits/stl_tree.h:1306:2: note: candidate: 'template<class _Kt, class _Req> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_find_tr(const _Kt&) [with _Kt = _Kt; _Req = _Req; _Key = unsigned int; _Val = std::pair<const unsigned int, unsigned int>; _KeyOfValue = std::_Select1st<std::pair<const unsigned int, unsigned int> >; _Compare = std::less<unsigned int>; _Alloc = std::allocator<std::pair<const unsigned int, unsigned int> >]'
 1306 |  _M_find_tr(const _Kt& __k)
      |  ^~~~~~~~~~
/usr/include/c  /9/bits/stl_tree.h:1306:2: note:   template argument deduction/substitution failed:
/usr/include/c  /9/bits/stl_tree.h: In substitution of 'template<class _Cmp, class _SfinaeType> using __has_is_transparent_t = typename std::__has_is_transparent<_Cmp, _SfinaeType>::type [with _Cmp = std::less<unsigned int>; _SfinaeType = std::pair<unsigned int, unsigned int>]':
/usr/include/c  /9/bits/stl_tree.h:1304:9:   required by substitution of 'template<class _Kt> decltype (((std::map<unsigned int, unsigned int>*)this)->std::map<unsigned int, unsigned int>::_M_t.std::_Rb_tree<unsigned int, std::pair<const unsigned int, unsigned int>, std::_Select1st<std::pair<const unsigned int, unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, unsigned int> > >::_M_find_tr(__x)) std::map<unsigned int, unsigned int>::find<_Kt>(const _Kt&) [with _Kt = std::pair<unsigned int, unsigned int>]'
/tmp/cpzeeEDm4C.cpp:10:52:   required from here
/usr/include/c  /9/bits/stl_tree.h:425:11: error: no type named 'type' in 'struct std::__has_is_transparent<std::less<unsigned int>, std::pair<unsigned int, unsigned int>, void>'
  425 |     using __has_is_transparent_t
      |           ^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c  /9/bits/stl_map.h: In substitution of 'template<class _Kt> decltype (((std::map<unsigned int, unsigned int>*)this)->std::map<unsigned int, unsigned int>::_M_t.std::_Rb_tree<unsigned int, std::pair<const unsigned int, unsigned int>, std::_Select1st<std::pair<const unsigned int, unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, unsigned int> > >::_M_find_tr(__x)) std::map<unsigned int, unsigned int>::find<_Kt>(const _Kt&) [with _Kt = std::pair<unsigned int, unsigned int>]':
/tmp/cpzeeEDm4C.cpp:10:52:   required from here
/usr/include/c  /9/bits/stl_tree.h:1315:2: note: candidate: 'template<class _Kt, class _Req> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_find_tr(const _Kt&) const [with _Kt = _Kt; _Req = _Req; _Key = unsigned int; _Val = std::pair<const unsigned int, unsigned int>; _KeyOfValue = std::_Select1st<std::pair<const unsigned int, unsigned int> >; _Compare = std::less<unsigned int>; _Alloc = std::allocator<std::pair<const unsigned int, unsigned int> >]'
 1315 |  _M_find_tr(const _Kt& __k) const
      |  ^~~~~~~~~~
/usr/include/c  /9/bits/stl_tree.h:1315:2: note:   template argument deduction/substitution failed:
In file included from /usr/include/c  /9/map:61,
                 from /usr/include/x86_64-linux-gnu/c  /9/bits/stdc  .h:81,
                 from /tmp/cpzeeEDm4C.cpp:3:
/usr/include/c  /9/bits/stl_map.h:1193:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator std::map<_Key, _Tp, _Compare, _Alloc>::find(const key_type&) const [with _Key = unsigned int; _Tp = unsigned int; _Compare = std::less<unsigned int>; _Alloc = std::allocator<std::pair<const unsigned int, unsigned int> >; std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<std::pair<const unsigned int, unsigned int> >; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = unsigned int]'
 1193 |       find(const key_type& __x) const
      |       ^~~~
/usr/include/c  /9/bits/stl_map.h:1193:28: note:   no known conversion for argument 1 from 'std::pair<unsigned int, unsigned int>' to 'const key_type&' {aka 'const unsigned int&'}
 1193 |       find(const key_type& __x) const
      |            ~~~~~~~~~~~~~~~~^~~
/usr/include/c  /9/bits/stl_map.h:1199:2: note: candidate: 'template<class _Kt> decltype (((const std::map<_Key, _Tp, _Compare, _Alloc>*)this)->std::map<_Key, _Tp, _Compare, _Alloc>::_M_t._M_find_tr(__x)) std::map<_Key, _Tp, _Compare, _Alloc>::find(const _Kt&) const [with _Kt = _Kt; _Key = unsigned int; _Tp = unsigned int; _Compare = std::less<unsigned int>; _Alloc = std::allocator<std::pair<const unsigned int, unsigned int> >]'
 1199 |  find(const _Kt& __x) const -> decltype(_M_t._M_find_tr(__x))
      |  ^~~~
/usr/include/c  /9/bits/stl_map.h:1199:2: note:   template argument deduction/substitution failed:
/usr/include/c  /9/bits/stl_map.h: In substitution of 'template<class _Kt> decltype (((const std::map<unsigned int, unsigned int>*)this)->std::map<unsigned int, unsigned int>::_M_t.std::_Rb_tree<unsigned int, std::pair<const unsigned int, unsigned int>, std::_Select1st<std::pair<const unsigned int, unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, unsigned int> > >::_M_find_tr(__x)) std::map<unsigned int, unsigned int>::find<_Kt>(const _Kt&) const [with _Kt = std::pair<unsigned int, unsigned int>]':
/tmp/cpzeeEDm4C.cpp:10:52:   required from here
/usr/include/c  /9/bits/stl_map.h:1199:2: error: no matching function for call to 'std::_Rb_tree<unsigned int, std::pair<const unsigned int, unsigned int>, std::_Select1st<std::pair<const unsigned int, unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, unsigned int> > >::_M_find_tr(const std::pair<unsigned int, unsigned int>&) const'
In file included from /usr/include/c  /9/map:60,
                 from /usr/include/x86_64-linux-gnu/c  /9/bits/stdc  .h:81,
                 from /tmp/cpzeeEDm4C.cpp:3:
/usr/include/c  /9/bits/stl_tree.h:1306:2: note: candidate: 'template<class _Kt, class _Req> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_find_tr(const _Kt&) [with _Kt = _Kt; _Req = _Req; _Key = unsigned int; _Val = std::pair<const unsigned int, unsigned int>; _KeyOfValue = std::_Select1st<std::pair<const unsigned int, unsigned int> >; _Compare = std::less<unsigned int>; _Alloc = std::allocator<std::pair<const unsigned int, unsigned int> >]'
 1306 |  _M_find_tr(const _Kt& __k)
      |  ^~~~~~~~~~
/usr/include/c  /9/bits/stl_tree.h:1306:2: note:   template argument deduction/substitution failed:
/usr/include/c  /9/bits/stl_tree.h:1315:2: note: candidate: 'template<class _Kt, class _Req> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_find_tr(const _Kt&) const [with _Kt = _Kt; _Req = _Req; _Key = unsigned int; _Val = std::pair<const unsigned int, unsigned int>; _KeyOfValue = std::_Select1st<std::pair<const unsigned int, unsigned int> >; _Compare = std::less<unsigned int>; _Alloc = std::allocator<std::pair<const unsigned int, unsigned int> >]'
 1315 |  _M_find_tr(const _Kt& __k) const
      |  ^~~~~~~~~~
/usr/include/c  /9/bits/stl_tree.h:1315:2: note:   template argument deduction/substitution failed:

How can I use uint32_t as key and value here?

CodePudding user response:

The problem is that std::map::find takes the key(which is of type Key) value of the element to search for as argument but you're providing a std::pair and hence the error.

So solve this just replace the call to find with:

//------------------------------v--->pass the key value
auto imageFoundIt = Mapper.find(a)

which will give return an iterator to an element with key equivalent to key. And if no such element is found, past-the-end iterator will be returned.

  • Related