C++ 中的映射错误

Map Error in C++

我正在使用 GCC 4.6 编译器,当我构建代码 (ns3) 时出现错误:

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 *’

这是因为我用了一张地图:

 map<uint32_t, uint32_t> dis;

我是这样插入的:

 dis.insert(p,t);

我想不出解决这个问题的方法。谢谢您的帮助。

您将 GCC 4.4 中的库与 GCC 4.6 一起使用 - 这可能是问题所在。

尝试将 4.6 headers 传递给编译器(如 -I/usr/include/c++/4.6)

我认为您没有按预期使用插入方法。我不确定,因为我看不到您正在使用的参数声明,但编译器说您正在使用该方法:

  void insert (InputIterator first, InputIterator last)

所以参数可能是同一个容器的迭代器。这里第一个参数指定要复制的容器的开始,最后一个参数标记此范围的结束(不包括最后一个元素)。 这些是您拥有的其他选项:

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

这是最常见的,我猜你想使用这个函数,它将元素 val 插入到地图中。这里 val 是一些类型为 pair<T1,T2> 的变量,其中 T1 和 T2 是您在创建映射时输入的参数(在您的例子中是 unsigned int,unsigned int)。您可以使用 std.

的函数 make_pair(first, second)
iterator insert (iterator position, const value_type& val);

最后一个与上一个类似,但它告诉地图新元素的位置可能在附近。但这只是出于性能目的的提示,因为 map 是一棵具有明确定义顺序的树。

因此您的代码可能类似于 dis.insert(make_pair(p,t));

此致。