【发布时间】:2026-02-05 03:15:01
【问题描述】:
我正在尝试使用this 答案创建内存映射文件,但出现编译错误。这是我的代码:
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);
结构是这样的:
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];
}
我得到的错误是:编译器无法从“boost::interprocess::offset_ptr”中推断出“_Ty*”的模板参数。我做错了什么?
【问题讨论】:
-
在不需要的地方使用原始指针和
struct Rectangle表示C 编程风格。请参阅我对一些现代化的回答。不过,更重要的是如何创建它的SSCCE。
标签: c++ boost memory-mapped-files interprocess