我有以下代码。尝试拥有一个具有字符串和数组结构的共享内存向量。但是当我编译代码时出现错误:
usr/local/include/boost/container/vector.hpp:1819:4: error: no matching function for call to ‘InData::InData(InData* const&)’
任何建议如何解决这个问题。我是 Boost 的新手。
#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;
}
如果只是做myvector->push_back(tData)
/usr/local/include/boost/container/detail/advanced_insert_int.hpp:166:10: error: no match for ‘operator=’ (operand types are ‘InData’ and ‘const value_type {aka const InData}’)
我已经对我的评论进行了实时编码:录制的会话
修复后的代码如下。一些注释(不过,请观看流媒体会议):
您未能为“共享”字符串提供分配器。这意味着它使用std::allocator
并且分配是从程序堆中完成的。哎呀。如果你幸运的话,你的程序将会死掉。
-
一旦你给出shared_string
正确的分配器,你会发现连锁反应。您需要在构造时传入分配器实例shared_string
因为进程间分配器实例是not默认可构造。
经验法则:有状态分配器是痛苦的。如果你避免痛苦,那么你就做错了(并且从错误的堆中分配)
-
命名很重要:
typedef allocator<InData, managed_shared_memory::segment_manager> tStructData;
为什么命名你的分配器类型tStructData
?你也可以称之为XXX
。所以,将其更改为
typedef allocator<InData, managed_shared_memory::segment_manager> salloc;
当我试图理解你的代码时,这花了我一段时间......
而不是显式创建alloc_inst
,考虑使用隐式转换segment_manager
-
The line
myvector->push_back(&tData);
试图推动pointer到类对象的向量中。无论您的分配器如何,这永远不会起作用
Live On Coliru
#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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)