gpt4 book ai didi

c++ - 共享内存 STL 映射

转载 作者:行者123 更新时间:2023-11-30 04:39:08 26 4
gpt4 key购买 nike

我正在用 C++ 编写 Apache 模块。我需要将所有 child 需要读取的公共(public)数据存储为共享内存的一部分。结构是一种 vector 映射,所以我想为它使用 STL 映射和 vector 。我为此编写了一个共享分配器和一个共享管理器,它们适用于 vector 但不适用于 map ,下面是示例:

typedef vector<CustomersData, SharedAllocator<CustomersData> > CustomerVector;
CustomerVector spData; //this one works fine

typedef SharedAllocator< pair< const int, CustomerVector > > PairAllocator;

typedef map<int, CustomerVector, less<int>, PairAllocator > SharedMap;

SharedMap spIndex; //this one doesn't work<

当我尝试使用第二个对象 (spIndex) 时出现编译时错误,类似于:

../SpatialIndex.h:97: error: '((SpatialIndex*)this)->SpatialIndex::spIndex' does not have class type

看起来编译器无法为 SharedMap 模板类型确定类型,这在我看来很奇怪,在我看来,所有模板参数都已指定。

你能帮忙吗?

谢谢本韦努托

您好,感谢您的评论。

SpatialIndex 是包含容器的类,它基本上由容器(SharedMap spIndex;它是 SpatialIndex 的成员)和两个方法 update 和 getData 构成。

在 update 方法中,下面的代码行给出了上面的编译器错误:

int spKey = this->calculateSpKey( customer.getLat(), customer.getLong() );
this->spIndex[spKey].push_back(customer);

改变最后一行的语法会改变编译器给出的错误,但基本上它表示它无法理解哪个类型的变量 spIndex,或者它无法为此类找到合适的重载构造函数。

最佳答案

请发布您初始化 spIndex 的行。编译器错误“没有类类型”通常意味着您将一个函数当作一个字段来引用,在这种情况下,这可能意味着您的编译器以某种方式将 spIndex 误认为是一个函数。我还没有看到代码,但我敢打赌 Most Vexing Parse 会以某种方式出现。

关于c++ - 共享内存 STL 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2561242/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com