我想将一个struct1包含在共享内存中,该struct1包含一个包含wstring的struct2矢量。所以:
我想将wchar和struct2分配器保留为类的成员,这样我就不必保留对shm的引用(因为我不必使用 shm.get_manager( )每次我想操作数据)。 但是无论我使用什么配置,它都会失败。
现在,我收到以下错误消息:
SharedData(InnerDataAllocator eAlloc):data(eAlloc) {
错误C2664:
'boost :: container :: vector,void> :: vector(const boost :: container :: vector,void>&)':无法将参数1从'InnerDataAllocator'转换为'const boost :: container :: new_allocator&'
header.h
typedef bip::allocator<wchar_t, bip::managed_shared_memory::segment_manager> CharAllocator;
typedef bip::basic_string<wchar_t, std::char_traits<wchar_t>, CharAllocator> MyShmString;
class InnerData {
public:
uint64_t id;
MyShmString name;
uint8_t status;
static const uint8_t defaultStatus = 3;
InnerData(CharAllocator cAlloc):name(cAlloc) {
}
};
typedef bip::allocator<InnerData, bip::managed_shared_memory::segment_manager> InnerDataAllocator;
class SharedData {
public:
int a;
bool newRequestUntreated = false;
bool newReplyUntreated = false;
bip::vector<InnerData> data;
SharedData(InnerDataAllocator eAlloc):data(eAlloc) {
}
};
UsualInnerData{//like InnerData but with normal std::wstring
public:
uint64_t id;
std::wstring name;
uint8_t status;
static const uint8_t status Default = 3;
};
class Zone {
public:
SharedData * sharedData;
CharAllocator charAllocator;
InnerDataAllocator innerDataAllocator;
void putInShm(const std::vector<UsualInnerData> &in);
void initializeZone(std::string &_name, std::string &_nameSeg);
static bool alreadyCreated;
};
code.cpp
void Zone::createNewCheckingZone(std::string &_name, std::string &_nameSeg) {
if (!alreadyCreated) {
int size = getOverallSize();
bip::managed_shared_memory shm(bip::create_only, _name.c_str(), 10000 + size);
CharAllocator scharAllocator(shm.get_segment_manager());
swap(charAllocator, scharAllocator); //use of swap because = operator says Not assignable from other allocator
InnerDataAllocator sinnerDataAllocator(shm.get_segment_manager());
swap(innerDataAllocator, sinnerDataAllocator);
sharedData = shm.construct<SharedData>(_nameSeg)(innerDataAllocator);
alreadyCreated = true;
}
return ;
}
void Zone::putInShm(const std::vector<UsualInnerData> &in){
InnerData newInnerData(charAllocator);
for (auto a : in) {
newInnerData.id = a.id;
newInnerData.name = a.xml.c_str();
newInnerData.status = InnerData::defaultStatus;
sharedData->data.push_back(newInnerData);
}
return;
}
我可以毫无问题地用Zone的charAllocator初始化InnerData.name,为什么我不能做同样的事情,即用Zone的innerDataAllocator初始化SharedData.data呢?
答案 0 :(得分:0)
实际上,我犯了一个错误,(我昨天整理完咖啡袋,现在开始感觉到),我定义了:
typedef bip::vector<InnerData, InnderDataAllocator> InnerDataVector;
,但没有使用它来定义SharedData.data。这是我的更改方式:
class SharedData {
public:
int a;
bool newRequestUntreated = false;
bool newReplyUntreated = false;
InnerDataVector data;
SharedData(InnerDataAllocator eAlloc):data(eAlloc) {}
};
现在它可以编译。