2010-04-01 96 views
1

我正在用C++编写Apache模块。我需要将所有孩子需要阅读的共同数据存储为共享内存的一部分。结构是一种向量图,所以我想使用STL图和向量。我写了一个共享分配,并为宗旨共享经理,他们工作的优良载体,但没有地图,下面是例子:共享内存STL地图

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。

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

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

改变最后一行的sintax不同编译器给出了错误,但基本上它说,它无法了解哪些类型变量spIndex是,或者它无法为此类找到适当的重载构造函数。

+1

欢迎。请通过缩进4个空格或1个选项卡来格式化您的代码。 – kennytm 2010-04-01 14:50:51

+0

'SpatialIndex'类型是什么样的?这是否在另一个类中声明了这些内容? – Thomas 2010-04-01 15:01:39

+0

'(SpatialIndex *)这是一个糟糕的主意。如果你绝对需要演员阵容(为什么,我不知道),请使用'static_cast'来确保你不会意外地执行'reinterpret_cast'或意外删除一个const限定符。 – 2010-04-01 18:27:26

回答

2

请发布初始化spIndex的行。编译器错误'没有类类型'通常意味着你指的是一个函数,就好像它是一个字段一样,在这种情况下,这可能意味着你的编译器以某种方式误解了函数的spIndex。我没有看到代码,但我敢打赌,最痛苦的解析会以某种方式出现。

0

非常感谢您的帮助,起始线确实

SharedMap spIndex(less<int>(), PairAllocator); 

这被解读由编译器的功能,它有两个参数,并返回一个SharedMap对象,从而导致以上所有问题。更改该行:

SharedMap spIndex; 

导致更intelegible错误,一种“无法找到该内部管理STL ::地图是stl_tree对象适当的构造函数”。

这其他错误解决了删除从以下行的“显性”字SharedAllocator.h

inline explicit SharedAllocator(SharedAllocator const&) {} 

template<typename U> 
inline explicit SharedAllocator(SharedAllocator<U> const&) {} 

我是错的考虑,它只是样板代码...