简体   繁体   English

C ++:Boost进程间内存映射文件错误

[英]C++: Boost interprocess memory mapped file error

I'm trying to create a memory mapped file using this answer, but I'm getting compile errors. 我正在尝试使用答案创建一个内存映射文件,但出现编译错误。 This is my code: 这是我的代码:

namespace bi = boost::interprocess;
std::string vecFile = "vector.dat";
bi::managed_mapped_file file_vec(bi::open_or_create,vecFile.c_str(), sizeof(struct Rectangle) * data_size);

typedef bi::allocator<struct Rectangle, bi::managed_mapped_file::segment_manager> rect_alloc;
typedef std::vector<struct Rectangle, rect_alloc>  MyVec;

MyVec * vecptr = file_vec.find_or_construct<MyVec>("myvector")(file_vec.get_segment_manager());

vecptr->push_back(random_rectangle);

The struct is this: 结构是这样的:

struct Rectangle{

  Rectangle(float *minArr, float *maxArr, int arr, int exp, int ID){
    this->arrival = arr;
    this->expiry = exp;
    this->id = ID;
    for(int i=0; i < 2; i++){
      min[i] = minArr[i];
      max[i] = maxArr[i];
    }

  int arrival, expiry, id;
  float min[2];
  float max[2];
}

The error I get is: Compiler could not deduce the template argument for '_Ty*' from 'boost::interprocess::offset_ptr'. 我得到的错误是:编译器无法从'boost :: interprocess :: offset_ptr'推导出'_Ty *'的模板参数。 What am I doing wrong? 我究竟做错了什么?

It looks okay to me: 在我看来还可以:

Live On Coliru 生活在Coliru

#include <boost/interprocess/managed_mapped_file.hpp>
#include <vector>

namespace bi = boost::interprocess;

struct Rectangle {
    Rectangle(float *minArr, float *maxArr, int arr, int exp, int ID) {
        this->arrival = arr;
        this->expiry = exp;
        this->id = ID;
        for (int i = 0; i < 2; i++) {
            min[i] = minArr[i];
            max[i] = maxArr[i];
        }
    };

    int arrival, expiry, id;
    float min[2];
    float max[2];
};

namespace Shared {
    using segment = bi::managed_mapped_file;
    using mgr     = segment::segment_manager;

    using alloc   = bi::allocator<Rectangle, mgr>;
    using vector  = std::vector<Rectangle, alloc>;
}

Rectangle random_rectangle() { 
    float dummy[2] = { };
    return { dummy, dummy, 0, 0, 0 }; 
}

int main() {
#define data_size 10
    std::string vecFile = "vector.dat";
    Shared::segment mmem(bi::open_or_create, vecFile.c_str(), (10u<<10) + sizeof(struct Rectangle) * data_size);

    Shared::vector *vecptr = mmem.find_or_construct<Shared::vector>("myvector")(mmem.get_segment_manager());

    vecptr->push_back(random_rectangle());
}

If it doesn't compile exactly as above, please note versions of your library and compiler. 如果编译不完全相同,请记下您的库和编译器的版本。 Consider upgrading. 考虑升级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM