boost中的共享内存向量
我有以下代码。尝试使用具有字符串和数组结构的共享内存向量。但是当我编译代码时,我得到了一个错误:boost中的共享内存向量,boost,vector,shared-memory,Boost,Vector,Shared Memory,我有以下代码。尝试使用具有字符串和数组结构的共享内存向量。但是当我编译代码时,我得到了一个错误: usr/local/include/boost/container/vector.hpp:1819:4: error: no matching function for call to ‘InData::InData(InData* const&)’ 任何关于如何解决这个问题的建议。我是个新手 #include <iostream> #include <string&g
usr/local/include/boost/container/vector.hpp:1819:4: error: no matching function for call to ‘InData::InData(InData* const&)’
任何关于如何解决这个问题的建议。我是个新手
#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>
using namespace boost::interprocess;
typedef boost::interprocess::basic_string<char> shared_string;
struct InData
{
int X,Y,H,W;
shared_string Label;
};
int main()
{
shared_memory_object::remove("MySharedMemory");
managed_shared_memory managed_shm(create_only, "MySharedMemory", 10000);
InData tData;// = managed_shm.construct<InData>("InDataStructure")();
tData.Label = "Hello World";
tData.H = 1;
tData.W = 1;
tData.Y = 1;
tData.X = 1;
typedef allocator<InData,managed_shared_memory::segment_manager> tStructData;
typedef vector<InData,tStructData> MyVector;
//Initialize shared memory STL-compatible allocator
tStructData alloc_inst (managed_shm.get_segment_manager());
//Construct a vector named "MyVector" in shared memory with argument alloc_inst
MyVector *myvector = managed_shm.construct<MyVector>("MyVector")(alloc_inst);
//InData data;
myvector->push_back(&tData);
return 0;
}
我对我的评论进行了现场编码:
#include <iostream>
#include <string>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/containers/vector.hpp>
using namespace boost::interprocess;
struct InData;
typedef 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() {
shared_memory_object::remove("MySharedMemory");
managed_shared_memory managed_shm(create_only, "MySharedMemory", 10000);
// Initialize shared memory STL-compatible allocator
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;
// Construct a vector named "MyVector" in shared memory with argument alloc_inst
typedef vector<InData, salloc> MyVector;
MyVector *myvector = managed_shm.construct<MyVector>("MyVector")(alloc_inst);
// InData data;
myvector->push_back(tData);
}
下面是固定代码。一些注释(请观看流媒体会话):
std::allocator
,并且分配是从程序堆中完成的。哎呀。如果你幸运的话,你的程序会死掉共享的\u字符串
一个合适的分配器,你就会发现连锁反应。您需要在构建shared_string
时传入分配器实例,因为进程间分配器实例是不可构建的
经验法则:有状态分配器是痛苦的。如果你避免了痛苦,那就是你做错了(并且从错误的堆中分配)
typedef allocator<InData, managed_shared_memory::segment_manager> tStructData;
这花了我一段时间来理解你的代码
alloc\u inst
,不如考虑使用段管理器的隐式转换
myvector->push_back(&tData);
尝试将指针推入类对象的向量中。无论您的分配器是什么,它都无法工作#include <iostream>
#include <string>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/containers/vector.hpp>
using namespace boost::interprocess;
struct InData;
typedef 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() {
shared_memory_object::remove("MySharedMemory");
managed_shared_memory managed_shm(create_only, "MySharedMemory", 10000);
// Initialize shared memory STL-compatible allocator
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;
// Construct a vector named "MyVector" in shared memory with argument alloc_inst
typedef vector<InData, salloc> MyVector;
MyVector *myvector = managed_shm.construct<MyVector>("MyVector")(alloc_inst);
// InData data;
myvector->push_back(tData);
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间boost::interprocess;
InData结构;
typedef分配器salloc;
typedef boost::进程间::基本\u字符串共享\u字符串;
因达结构{
int X,Y,H,W;
InData(salloc alloc):标签(alloc){}
共享字符串标签;
};
int main(){
共享内存对象::删除(“MySharedMemory”);
托管共享内存托管shm(仅创建“MySharedMemory”,10000);
//初始化共享内存STL兼容分配器
salloc alloc________________________管理器());
InData tData(alloc_inst);/=托管结构(“InDataStructure”)();
tData.Label=“你好世界”;
tData.H=1;
t数据W=1;
t数据Y=1;
tData.X=1;
//使用参数alloc_inst在共享内存中构造一个名为“MyVector”的向量
typedef向量MyVector;
MyVector*MyVector=管理型结构(“MyVector”)(alloc_inst);
//InData数据;
myvector->push_back(tData);
}
伙计们,这是个好问题。一切都在那里。这不是“为我调试代码”的问题。谢谢!但我仍然得到了我提到的第二个错误:错误:与“operator=”不匹配(操作数类型为“InData”和“const value_type{aka const InData}”),请分享您这次使用的确切代码。您可以清楚地看到,我的代码(以及两个编译器和两个不同的标准库实现)并没有做到这一点,我使用的代码与您更改的代码完全相同,但仍然会出现错误。此外,我如何向结构添加动态数组,如float*@Mrutyunjay编译器/库版本?动态数组:vector
#include <iostream>
#include <string>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/containers/vector.hpp>
using namespace boost::interprocess;
struct InData;
typedef 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() {
shared_memory_object::remove("MySharedMemory");
managed_shared_memory managed_shm(create_only, "MySharedMemory", 10000);
// Initialize shared memory STL-compatible allocator
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;
// Construct a vector named "MyVector" in shared memory with argument alloc_inst
typedef vector<InData, salloc> MyVector;
MyVector *myvector = managed_shm.construct<MyVector>("MyVector")(alloc_inst);
// InData data;
myvector->push_back(tData);
}