samedi 16 mai 2020

Unable to insert to std map

I have a std map consisting of int and pointer to a struct. However using the insert function I am unable to add the same. The error message seems bit too tricky for me to get to the bottom.

#include <iostream>
#include <map>
using namespace std;

typedef struct 
{

}NODE;

int main()
{
    std::map<int, NODE*> mp;
    int i = 0;
    mp.insert(i, new NODE());

    return 0;
}

The error language is bit cryptic. I am trying to add an int and a NODE pointer to map of int and NODE pointer. Why is this giving me un-matching function?

main.cpp: In function ‘int main()’:
main.cpp:22:28: error: no matching function for call to ‘std::map::insert(int&, NODE*)’
     mp.insert(i, new NODE());
                            ^
In file included from /usr/include/c++/7/map:61:0,
                 from main.cpp:10:
/usr/include/c++/7/bits/stl_map.h:621:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>::insert_return_type std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::node_type&&) [with _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >; std::map<_Key, _Tp, _Compare, _Alloc>::insert_return_type = std::_Node_insert_return >, std::_Node_handle, std::allocator > > > >; std::map<_Key, _Tp, _Compare, _Alloc>::node_type = std::_Node_handle, std::allocator > > >]
       insert(node_type&& __nh)
       ^~~~~~
/usr/include/c++/7/bits/stl_map.h:621:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/7/bits/stl_map.h:626:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator, std::map<_Key, _Tp, _Compare, _Alloc>::node_type&&) [with _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >; std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator >; std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator >; std::map<_Key, _Tp, _Compare, _Alloc>::node_type = std::_Node_handle, std::allocator > > >]
       insert(const_iterator __hint, node_type&& __nh)
       ^~~~~~
/usr/include/c++/7/bits/stl_map.h:626:7: note:   no known conversion for argument 1 from ‘int’ to ‘std::map::const_iterator {aka std::_Rb_tree_const_iterator >}’
/usr/include/c++/7/bits/stl_map.h:795:7: note: candidate: std::pair, std::_Select1st >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const value_type&) [with _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >; typename std::_Rb_tree<_Key, std::pair, std::_Select1st >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind >::other>::iterator = std::_Rb_tree_iterator >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair]
       insert(const value_type& __x)
       ^~~~~~
/usr/include/c++/7/bits/stl_map.h:795:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/7/bits/stl_map.h:802:7: note: candidate: std::pair, std::_Select1st >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::value_type&&) [with _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >; typename std::_Rb_tree<_Key, std::pair, std::_Select1st >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind >::other>::iterator = std::_Rb_tree_iterator >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair]
       insert(value_type&& __x)
       ^~~~~~
/usr/include/c++/7/bits/stl_map.h:802:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/7/bits/stl_map.h:808:2: note: candidate: template std::__enable_if_t, _Pair>::value, std::pair, std::_Select1st >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind >::other>::iterator, bool> > std::map<_Key, _Tp, _Compare, _Alloc>::insert(_Pair&&) [with _Pair = _Pair; _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >]
  insert(_Pair&& __x)
  ^~~~~~
/usr/include/c++/7/bits/stl_map.h:808:2: note:   template argument deduction/substitution failed:
main.cpp:22:28: note:   candidate expects 1 argument, 2 provided
     mp.insert(i, new NODE());
                            ^
In file included from /usr/include/c++/7/map:61:0,
                 from main.cpp:10:
/usr/include/c++/7/bits/stl_map.h:822:7: note: candidate: void std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::initializer_list >) [with _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >]
       insert(std::initializer_list<value_type> __list)
       ^~~~~~
/usr/include/c++/7/bits/stl_map.h:822:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/7/bits/stl_map.h:852:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator, const value_type&) [with _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >; std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator >; std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair]
       insert(const_iterator __position, const value_type& __x)
       ^~~~~~
/usr/include/c++/7/bits/stl_map.h:852:7: note:   no known conversion for argument 1 from ‘int’ to ‘std::map::const_iterator {aka std::_Rb_tree_const_iterator >}’
/usr/include/c++/7/bits/stl_map.h:862:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator, std::map<_Key, _Tp, _Compare, _Alloc>::value_type&&) [with _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >; std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator >; std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair]
       insert(const_iterator __position, value_type&& __x)
       ^~~~~~
/usr/include/c++/7/bits/stl_map.h:862:7: note:   no known conversion for argument 1 from ‘int’ to ‘std::map::const_iterator {aka std::_Rb_tree_const_iterator >}’
/usr/include/c++/7/bits/stl_map.h:867:2: note: candidate: template std::__enable_if_t, _Pair>::value, typename std::_Rb_tree<_Key, std::pair, std::_Select1st >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind >::other>::iterator> std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator, _Pair&&) [with _Pair = _Pair; _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >]
  insert(const_iterator __position, _Pair&& __x)
  ^~~~~~
/usr/include/c++/7/bits/stl_map.h:867:2: note:   template argument deduction/substitution failed:
main.cpp:22:28: note:   cannot convert ‘i’ (type ‘int’) to type ‘std::map::const_iterator {aka std::_Rb_tree_const_iterator >}’
     mp.insert(i, new NODE());
                            ^
In file included from /usr/include/c++/7/map:61:0,
                 from main.cpp:10:
/usr/include/c++/7/bits/stl_map.h:885:2: note: candidate: template void std::map<_Key, _Tp, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >]
  insert(_InputIterator __first, _InputIterator __last)
  ^~~~~~
/usr/include/c++/7/bits/stl_map.h:885:2: note:   template argument deduction/substitution failed:
main.cpp:22:28: note:   deduced conflicting types for parameter ‘_InputIterator’ (‘int’ and ‘NODE*’)
     mp.insert(i, new NODE());

Aucun commentaire:

Enregistrer un commentaire