在共享内存中的boost图中插入boost矢量

时间:2011-12-15 08:27:47

标签: c++ boost ipc shared-memory boost-interprocess

这是我到目前为止所拥有的。但这甚至没有编译。我该怎么做才能让它发挥作用?

typedef allocator<int, managed_shared_memory::segment_manager> vecAllocator;
typedef vector<int, vecAllocator> vec;
typedef std::pair<const int, vec> ValueType;
typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;
typedef multimap<int, vec, std::less<int>, ShmemAllocator> MyMap;

ShmemAllocator alloc_inst (segment.get_segment_manager());
vecAllocator vectorallocator (segment.get_segment_manager());

MyMap *mymap = segment.construct<MyMap>("MyMap")(std::less<int>(),alloc_inst);
vec *myvec = segment.construct<vec>("myvec")(std::less<int>(), vectorallocator);
myvec->push_back(10);

我现在如何继续将此向量插入地图? 这是我尝试过的事情之一

mymap->insert(std::pair<const int, vec>(i, myvec));

但似乎没有用。我知道在共享内存中创建指向向量的指针不是正确的方法。如果我要将其插入地图,我需要为此创建一个对象。但是如何做到这一点或至少如何将这些东西融入共享内存?

g ++抛出的错误是:

sharedMap.cpp:46: no matching function for call to ‘std::pair<const int, boost::container::vector<int, boost::interprocess::allocator<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> > > >::pair(int&, main()::vec*&)’
    /usr/include/c++/4.2.1/bits/stl_pair.h:84: note: candidates are: std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = const int, _T2 = boost::container::vector<int, boost::interprocess::allocator<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> > >]
    /usr/include/c++/4.2.1/bits/stl_pair.h:80: note:                 std::pair<_T1, _T2>::pair() [with _T1 = const int, _T2 = boost::container::vector<int, boost::interprocess::allocator<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> > >]
    /usr/include/c++/4.2.1/bits/stl_pair.h:69: note:                 std::pair<const int, boost::container::vector<int, boost::interprocess::allocator<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> > > >::pair(const std::pair<const int, boost::container::vector<int, boost::interprocess::allocator<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> > > >&)
    /usr/local/include/boost/preprocessor/iteration/detail/local.hpp: In member function ‘void boost::interprocess::detail::Ctor2Arg<T, is_iterator, P0, P1>::construct(void*, boost::interprocess::detail::false_) [with T = boost::container::vector<int, boost::interprocess::allocator<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> > >, bool is_iterator = false, P0 = std::less<int>, P1 = boost::interprocess::allocator<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> >]’:
    /usr/local/include/boost/preprocessor/iteration/detail/local.hpp:40:   instantiated from ‘void boost::interprocess::detail::Ctor2Arg<T, is_iterator, P0, P1>::construct_n(void*, size_t, size_t&) [with T = boost::container::vector<int, boost::interprocess::allocator<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> > >, bool is_iterator = false, P0 = std::less<int>, P1 = boost::interprocess::allocator<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> >]’
    sharedMap.cpp:54:   instantiated from here
    /usr/local/include/boost/preprocessor/iteration/detail/local.hpp:40: error: no matching function for call to ‘boost::container::vector<int, boost::interprocess::allocator<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> > >::vector(const std::less<int>&, const boost::interprocess::allocator<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> >&)’
    /usr/local/include/boost/interprocess/containers/container/vector.hpp:474: note: candidates are: boost::container::vector<T, A>::vector(boost::interprocess::rv<boost::container::vector<T, A> >&) [with T = int, A = boost::interprocess::allocator<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> >]
    /usr/local/include/boost/interprocess/containers/container/vector.hpp:465: note:                 boost::container::vector<T, A>::vector(const boost::container::vector<T, A>&) [with T = int, A = boost::interprocess::allocator<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> >]
    /usr/local/include/boost/interprocess/containers/container/vector.hpp:456: note:                 boost::container::vector<T, A>::vector(typename A::size_type, const T&, const A&) [with T = int, A = boost::interprocess::allocator<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> >]
    /usr/local/include/boost/interprocess/containers/container/vector.hpp:445: note:                 boost::container::vector<T, A>::vector(typename A::size_type) [with T = int, A = boost::interprocess::allocator<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> >]
    /usr/local/include/boost/interprocess/containers/container/vector.hpp:434: note:                 boost::container::vector<T, A>::vector(const A&) [with T = int, A = boost::interprocess::allocator<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> >]

第54行是main中的最后一行(返回)

第46行是mymap-&gt;插入行。

1 个答案:

答案 0 :(得分:2)

问题在于:

vec *myvec = segment.construct<vec>("myvec")(std::less<int>(), vectorallocator);

传递std::less<int>实例没有用处。试试这个:

vec *myvec = segment.construct<vec>("myvec")(vectorallocator);