1

我正在尝试将一些值插入到存储在共享内存中的 boost::interprocess::map 中。

问题是当我尝试编译它时它给了我“对重载函数的模棱两可的调用”,我不知道为什么。以下是一些代码片段:

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

typedef char * KeyType;
typedef long  ValueType;
typedef std::pair<const char *, long> Type_Value;

typedef boost::interprocess::allocator<Type_Value, boost::interprocess::managed_shared_memory::segment_manager> ShmemAllocator;
typedef boost::interprocess::map<KeyType, ValueType, std::less<KeyType>, ShmemAllocator> MyMap;


...

MyMap * myMap = segment.construct<MyMap>("CyValoresRTD")      //object name
                         (std::less<char *>() //first  ctor parameter
                         ,alloc_inst);     //second ctor parameter

...
char * text = "some text";
long value = 1234;
myMap->insert( std::make_pair(text, value) );

这个插入调用给了我一些错误:

vcrtdserverimpl.cpp(445) : error C2668: 'boost::interprocess_container::map<Key,T,Pred,Alloc>::insert' : ambiguous call to overloaded function
with
[
        Key=VCRTDServer::KeyType,
        T=VCRTDServer::ValueType,
        Pred=std::less<VCRTDServer::KeyType>,
        Alloc=VCRTDServer::ShmemAllocator
]
p:\lib\boost_1_39_0\boost\interprocess\containers\container\map.hpp(407): could be 'std::pair<_Ty1,_Ty2> boost::interprocess_container::map<Key,T,Pred,Alloc>::insert(const std::pair<char *,long> &)'
with
[
        _Ty1=boost::interprocess_container::containers_detail::rbtree<VCRTDServer::KeyType ,std::pair<const VCRTDServer::KeyType ,VCRTDServer::ValueType>,boost::interprocess_container::containers_detail::select1st<std::pair<const VCRTDServer::KeyType ,VCRTDServer::ValueType>>,std::less<VCRTDServer::KeyType>,VCRTDServer::ShmemAllocator>::iterator,
        _Ty2=bool,
        Key=VCRTDServer::KeyType,
        T=VCRTDServer::ValueType,
        Pred=std::less<VCRTDServer::KeyType>,
        Alloc=VCRTDServer::ShmemAllocator
]
p:\lib\boost_1_39_0\boost\interprocess\containers\container\map.hpp(396): or       'std::pair<_Ty1,_Ty2> boost::interprocess_container::map<Key,T,Pred,Alloc>::insert(const std::pair<const Key,T> &)'
with
[
        _Ty1=boost::interprocess_container::containers_detail::rbtree<VCRTDServer::KeyType ,std::pair<const VCRTDServer::KeyType ,VCRTDServer::ValueType>,boost::interprocess_container::containers_detail::select1st<std::pair<const VCRTDServer::KeyType ,VCRTDServer::ValueType>>,std::less<VCRTDServer::KeyType>,VCRTDServer::ShmemAllocator>::iterator,
        _Ty2=bool,
        Key=VCRTDServer::KeyType,
        T=VCRTDServer::ValueType,
        Pred=std::less<VCRTDServer::KeyType>,
        Alloc=VCRTDServer::ShmemAllocator
]
while trying to match the argument list '(std::pair<_Ty1,_Ty2>)'
with
[
        _Ty1=char *,
        _Ty2=int
]

有没有人有任何想法?

4

1 回答 1

1

错误消息告诉您问题所在。

could be ...
::insert(const std::pair<char *,long> &)'

or ....
::insert(const std::pair<const Key,T> &)'

 while trying to match the argument list '(std::pair<_Ty1,_Ty2>)'
            with
            [
                    _Ty1=char *,
                    _Ty2=int
            ]

std::map键是常量,插入的对也是常量。你可能会更好地使用 std::string作为一个关键。

于 2012-06-18T21:53:04.893 回答