Boost 共享结构中的 Float* 或 Vector

Float* or Vector in Boost shared structure

我有以下代码编写结构来提升共享内存。我无法添加新的 float* 或 Vector。 boost version:1.59.0 得到一堆错误

error: no matching function for call to ‘boost::interprocess::allocator<boost::container::container_detail::list_node<fl‌​oat, boost::interprocess::offset_ptr<void>......." 

尝试过:

typedef boost::interprocess::list<float, salloc::rebind<float>::other> shared_float;

关于如何在结构中添加 float* 或向量的任何建议,这些结构将被添加以增加共享内存

#include <iostream>
#include <string>
#include <cstdlib>
#include <boost/interprocess/ipc/message_queue.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/shared_array.hpp>
#include <boost/interprocess/containers/list.hpp>
#include <boost/version.hpp>

using namespace boost::interprocess;


struct InData;
typedef boost::interprocess::allocator<InData, managed_shared_memory::segment_manager> salloc;
typedef boost::interprocess::basic_string<char, std::char_traits<char>, salloc::rebind<char>::other> shared_string;

struct InData {
    int X, Y, H, W;

    InData(salloc alloc) : Label(alloc) {}
    shared_string Label;
};

int main() {

    std::cout << "Using Boost "
              << BOOST_VERSION / 100000     << "."  // major version
              << BOOST_VERSION / 100 % 1000 << "."  // minor version
              << BOOST_VERSION % 100                // patch level
              << std::endl;

    shared_memory_object::remove("MySharedMemory");
    managed_shared_memory managed_shm(create_only, "MySharedMemory", 10000);

    salloc alloc_inst(managed_shm.get_segment_manager());

    InData tData(alloc_inst); // = managed_shm.construct<InData>("InDataStructure")();

    tData.Label = "Hello World";
    tData.H = 1;
    tData.W = 1;
    tData.Y = 1;
    tData.X = 1;

    typedef boost::interprocess::list<InData, salloc> MyList;
    MyList *myvector = managed_shm.construct<MyList>("MyVector")(alloc_inst);

    myvector->push_back(tData);
}

这编译,看起来像你想要的:

Live On Coliru

#include <iostream>
#include <string>
#include <cstdlib>
#include <boost/interprocess/ipc/message_queue.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/shared_array.hpp>
#include <boost/interprocess/containers/list.hpp>
#include <boost/version.hpp>

using namespace boost::interprocess;


struct InData;
typedef boost::interprocess::allocator<InData, managed_shared_memory::segment_manager> salloc;
typedef boost::interprocess::basic_string<char, std::char_traits<char>, salloc::rebind<char>::other> shared_string;
typedef boost::interprocess::vector<float, salloc::rebind<float>::other> shared_float_vec;

struct InData {
    int X, Y, H, W;

    InData(salloc alloc) : Label(alloc), Floats(alloc) {}
    shared_string Label;
    shared_float_vec Floats;
};

int main() {

    std::cout << "Using Boost "
              << BOOST_VERSION / 100000     << "."  // major version
              << BOOST_VERSION / 100 % 1000 << "."  // minor version
              << BOOST_VERSION % 100                // patch level
              << std::endl;

    shared_memory_object::remove("MySharedMemory");
    managed_shared_memory managed_shm(create_only, "MySharedMemory", 10000);

    salloc alloc_inst(managed_shm.get_segment_manager());

    InData tData(alloc_inst); // = managed_shm.construct<InData>("InDataStructure")();

    tData.Label = "Hello World";
    tData.Floats.push_back(3.14);
    tData.H = 1;
    tData.W = 1;
    tData.Y = 1;
    tData.X = 1;

    typedef boost::interprocess::list<InData, salloc> MyList;
    MyList *myvector = managed_shm.construct<MyList>("MyVector")(alloc_inst);

    myvector->push_back(tData);
}