C++ boost中不明确的map.insert

C++ boost中不明确的map.insert,c++,boost,C++,Boost,我试图在boost映射中插入一个值,但是在boost映射插入中对insert语句的调用是不明确的 #include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/containers/map.hpp> #include <boost/interprocess/allocators/allocator.hpp> #include <functional

我试图在boost映射中插入一个值,但是在boost映射插入中对insert语句的调用是不明确的

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <functional>
#include <utility>

using namespace boost::interprocess;

    int main ()
    {

    // remove earlier existing SHM
    shared_memory_object::remove("SharedMemoryName");

    // create new
    managed_shared_memory segment(create_only,"SharedMemoryName",65536);

    //Note that map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>,
    //so the allocator must allocate that pair.
    typedef std::string KeyType;
    typedef map<std::string, int>  MappedType;
    typedef std::pair<const KeyType, MappedType> ValueType;

    //allocator of for the map.
    typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;


    //Initialize the shared memory STL-compatible allocator
    ShmemAllocator alloc_inst (segment.get_segment_manager());

    //third parameter argument is the ordering function is used to compare the keys.
    typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MySHMMap;

    //offset ptr within SHM for map
    offset_ptr<MySHMMap> m_pmap = segment.construct<MySHMMap>("MySHMMapName")(std::less<std::string>(), alloc_inst);

    //Insert data in the map
           std::string my_string = "test";
            m_pmap[0].insert(std::make_pair( my_string, 0) );


    return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间boost::interprocess;
int main()
{
//删除先前的现有SHM
共享内存对象::删除(“SharedMemoryName”);
//创造新的
托管共享内存段(仅创建“SharedMemoryName”,65536);
//请注意,映射的值类型是std::pair,
//因此分配器必须分配该对。
typedef std::字符串键类型;
类型定义映射类型;
typedef std::pair ValueType;
//映射的分配器。
typedef分配器ShmemAllocator;
//初始化共享内存STL兼容分配器
ShmemAllocator alloc_inst(段.get_段管理器());
//第三个参数参数是排序函数,用于比较键。
typedef-map-MySHMMap;
//用于map的SHM内的偏移ptr
offset_ptr m_pmap=segment.construct(“MySHMMapName”)(std::less(),alloc_inst);
//在地图中插入数据
std::string my_string=“test”;
m_pmap[0]。插入(std::make_pair(my_string,0));
返回0;
}
被调用的boost API包括:

    std::pair<iterator,bool> insert(const nonconst_value_type& x)
    std::pair<iterator,bool> insert(const value_type& x)
    typedef typename tree_t::value_type             value_type;
    typedef std::pair<key_type, mapped_type>        nonconst_value_type;
std::成对插入(常量非常量值类型&x)
标准::对插入(常量值\u类型和x)
typedef typename tree_t::value_type value_type;
typedef std::对非ST_值_类型;
错误日志:

/home/user/droy/src/quotes/shmmutimap/src/writer.cxx:39: error:  call of overloaded 'insert(std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int>)' is ambiguous
/home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/interprocess/containers/container/map.hpp:410: note: candidates are: std::pair<typename boost::container::containers_detail::rbtree<Key, std::pair<const Key, T>, boost::container::containers_detail::select1st<std::pair<const Key, T> >, Pred, Alloc>::iterator, bool> boost::container::map<Key, T, Pred, Alloc>::insert(const typename boost::container::containers_detail::rbtree<Key, std::pair<const Key, T>, boost::container::containers_detail::select1st<std::pair<const Key, T> >, Pred, Alloc>::value_type&) [with Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, T = boost::container::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > >, Pred = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, Alloc = boost::interprocess::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::container::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> >]
/home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/interprocess/containers/container/map.hpp:421: note:                 std::pair<typename boost::container::containers_detail::rbtree<Key, std::pair<const Key, T>, boost::container::containers_detail::select1st<std::pair<const Key, T> >, Pred, Alloc>::iterator, bool> boost::container::map<Key, T, Pred, Alloc>::insert(const std::pair<typename boost::container::containers_detail::rbtree<Key, std::pair<const Key, T>, boost::container::containers_detail::select1st<std::pair<const Key, T> >, Pred, Alloc>::key_type, T>&) [with Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, T = boost::container::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > >, Pred = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, Alloc = boost::interprocess::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::container::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> >]
/home/user/droy/src/quotes/shmmutimap/src/writer.cxx:39:错误:重载的“insert(std::pair)”调用不明确
/home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/interprocess/containers/container/map.hpp:410:注意:候选项是:std::pair boost::container::map::insert(const typename boost::container::containers\u detail::rbtree::value\u type&)[使用Key=std::basic_字符串,T=boost::container::map,Pred=std::less,Alloc=boost::进程间::分配器]
/home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/interprocess/containers/container/map.hpp:421:note:std::pair boost::container::map::insert(const std::pair&)[带Key=std::basic_字符串,T=boost::container::map,Pred=std::less,Alloc=boost::interprocess::分配器]

insert语句的参数最终都是相同的类型,我如何解决这个问题,有什么提示吗?

现在,您创建了一个
映射。我想您的意图是创建一个
映射。
。要做到这一点,您需要对代码进行非常简单的修复:

// typedef map<std::string, int>  MappedType;
typedef int MappedType;
更新: 由于我的直觉似乎是错误的,而您确实想创建一个
地图
,因此响应略有不同

请注意,您的呼叫基本上是在尝试这样做:

map<string, int> x(0);
m_pmap[0][my_string] = x;

当询问编译器错误时,总是完整地引用确切的编译器错误以及给出错误的行。还包括周围的编译器输出,它通常包含“注释”这进一步解释了错误。我已经根据您的建议更新了代码和错误日志。谢谢!发布了#include和namespace。嗨,比尔,谢谢您的输入,但是我的意图是创建一个地图,我会告诉您我为什么要这样做,我有一个国家代码,对于每个国家代码,我们都有符号,我想跟踪的时间of、 我在下面贴了一个例子。美国-ABC-20:30,DEF-12:30,澳大利亚-UFG-23:20,XGF-16:30,稍后我想做一些时间部分的计算。谢谢!必须有一个解决方法来呼叫一个特定类型的过载操作员,我想,我不确定我将如何做。我遇到了这个帖子,但无法向m申请我的情况很糟糕,谢谢。
map<string, int> x(0);
m_pmap[0][my_string] = x;
m_pmap[0].insert(std::make_pair( my_string, map<std::string, int>()));
m_pmap[0].insert(std::make_pair( my_string, MappedType()));
m_pmap[0][my_string] = map<std::string, int>();
m_pmap[0][my_string] = MappedType();
m_pmap[0]["us"]["abc"] = 2030;
m_pmap[0]["us"]["def"] = 1230;