c++dictionarybuildstlwaf

Map Error in C++


I am using GCC 4.6 compiler and when I build my code (ns3) I get the error:

In file included from /usr/include/c++/4.4/map:60,
                 from ../src/internet-stack/tcp-typedefs.h:23,
                 from ../src/internet-stack/mp-tcp-socket-impl.cc:16:
/usr/include/c++/4.4/bits/stl_tree.h: In member function ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_II, _II) [with _InputIterator = unsigned int, _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> >]’:
/usr/include/c++/4.4/bits/stl_map.h:553:   instantiated from ‘void std::map<_Key, _Tp, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = unsigned int, _Key = unsigned int, _Tp = unsigned int, _Compare = std::less<unsigned int>, _Alloc = std::allocator<std::pair<const unsigned int, unsigned int> >]’
../src/internet-stack/mp-tcp-socket-impl.cc:1536:   instantiated from here
/usr/include/c++/4.4/bits/stl_tree.h:1324: error: invalid type argument of ‘unary *’

This is because I used a map:

 map<uint32_t, uint32_t> dis;

in which I inserted like this:

 dis.insert(p,t);

I can't figure out a way to resolve this. Thanks for the help.


Solution

  • I think you are not using the insert method like you are supposed to do. I am not sure because i can't see the declaration of the arguments that you are using, but the compiler is saying that you are using the method:

      void insert (InputIterator first, InputIterator last)
    

    so the arguments may be iterators of the same container. Here the first argument designates the begin of the container to be copied and the last argument marks the end of this range (not including the last element). These are the others options you have:

    pair<iterator,bool> insert (const value_type& val);
    

    This is the most usual and i guess you want to use this function, it inserts the element val into the map. Here val is some variable with type pair<T1,T2> where T1 and T2 are the arguments that you put in when creating the map (in your case unsigned int, unsigned int). You can use the function make_pair(first, second) of the std.

    iterator insert (iterator position, const value_type& val);
    

    The last one is like the previous but it tells to the map that the position of the new element may be near position. But this is just a hint for perfomance purpose since map is a tree with a well-defined order.

    So your code might be something like dis.insert(make_pair(p,t));.

    Regards.