这是我用来测试以下内容的代码:
#include <iostream>
#include <boost/interprocess/managed_mapped_file.hpp>
#include <boost/container/map.hpp>
#include <boost/interprocess/managed_external_buffer.hpp>
#include <boost/interprocess/allocators/node_allocator.hpp>
#include <boost/container/vector.hpp>
namespace bi = boost::interprocess;
int main() {
    bi::managed_mapped_file mmfile(bi::open_or_create, "map_iv.dat", 10000000);
    typedef bi::allocator<int, bi::managed_mapped_file::segment_manager> int_allocator;
    typedef boost::container::vector<int, int_allocator>  MyVec;
    typedef std::pair<const std::string, MyVec> MyPair;
    typedef std::less<std::string> MyLess;
    typedef bi::node_allocator<MyPair, bi::managed_mapped_file::segment_manager> node_allocator_t;
    typedef boost::container::map<std::string, MyVec, std::less<std::string>, node_allocator_t> MyMap;
    MyMap * mapptr = mmfile.find_or_construct<MyMap>("mymap")(mmfile.get_segment_manager());
    (*mapptr)["Hello"].push_back(17);
    long long s = mapptr->size();
    std::cout << s << ' ';
    std::cout << (*mapptr)["World"][0] << ' ';
    return 0;
}我在Visual 2017中得到了以下错误消息:
boost_1_69_0\boost\container\vector.hpp(294): error C2512:
"boost::interprocess::allocator<int,boost::interprocess::segment_manager<CharType,MemoryAlgorithm,IndexType>>::allocator": no appropriate default constructor available我正试图让它运行,并将感谢任何帮助!
发布于 2019-11-04 18:35:00
我认为问题在于它不能为向量构造分配器。您可以定义一个分配程序类,它有一个默认的ctor,它使用mmfile.get_segment_manager() (然后您需要通过一些静态数据来设置它)。但是,可以使用[]操作符来添加新值,而可以使用emplace进行快速修复。
auto it = mapptr->emplace("Hello", mmfile.get_segment_manager()).first;
it->second.push_back(17);对不起,我还没试过呢。
https://stackoverflow.com/questions/58647252
复制相似问题