问题描述
我有以下代码.尝试使用结构包含字符串和数组的共享内存向量.但是当我编译代码时,我得到了错误:
I have the following code. Trying to have a shared memory vector with structure having string and arrays. But when i compile the code I am getting error:
usr/local/include/boost/container/vector.hpp:1819:4: error: no matching function for call to ‘InData::InData(InData* const&)’
关于如何解决此问题的任何建议.我是Boost的新手.
Any suggestion how to fix this. I am new to 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}’)
推荐答案
我已经对我的评论进行了现场编码: 录制的会话
I've live-coded my review: recorded session
固定的代码如下.一些注意事项(不过请观看流媒体会话):
The fixed up code is below. Some of the notes (please watch the streaming session, though):
-
您未能为共享"字符串分配分配器.这意味着它使用
std::allocator
,并且分配是从程序堆完成的. OOOPS.如果幸运的话,您的程序将终止.
you failed to give the "shared" string an allocator. That means it uses
std::allocator
and the allocations are done from the program heap. OOOPS. Your program will die, if you're lucky.
一旦给shared_string
适当的分配器,就会发现涟漪效应.您需要在构造shared_string
时传递分配器实例,因为进程间分配器实例默认不是可构造的.
once you give the shared_string
the proper allocator, you'll find ripple effects. You need to pass in allocator instances at construction of shared_string
because interprocess allocator instances are not default constructible.
命名很重要:
naming is important:
typedef allocator<InData, managed_shared_memory::segment_manager> tStructData;
为什么命名您的分配器类型为tStructData
?您最好将其称为XXX
.因此,将其更改为
why name your allocator type tStructData
? You might as well call it XXX
. So, change it to
typedef allocator<InData, managed_shared_memory::segment_manager> salloc;
考虑使用从segment_manager
行
myvector->push_back(&tData);
尝试将 pointer 推入类对象的向量中.不管您的分配器是什么,它永远都行不通
tries to push a pointer into a vector of class objects. That can never work, regardless of your allocator(s)
#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);
}
这篇关于共享内存向量的提升的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!