【发布时间】:2013-07-30 09:37:33
【问题描述】:
我正在尝试实现一个自定义 C++ 比较函数,以传递给 std::map。
按照map API 中的说明,我实现了:
35 typedef std::pair<uint64_t, KeyHash> TabletKey;
36
37 class CmpTabletKey {
38 public:
39 bool operator()(const TabletKey& key1, const TabletKey& key2) const {
40 if (!(key1.first < key2.first)) {
41 return false;
42 }
43 if (!(key2.first < key1.first)) {
44 return false;
45 }
46
47 return true;
48 }
49 };
在map 是属性的类中,我有:
55 class ObjectFinder {
56 public:
57 class TableConfigFetcher; // forward declaration, see full declaration below
58 class CmpTabletKey;
// .. more code here
private:
97 std::map<TabletKey, ProtoBuf::Tablets::Tablet, CmpTabletKey> tableMap;
}
我收到以下错误:
/home/ribeiro.phillipe/ramcloud/src/ObjectFinder.h:97: instantiated from here
/usr/lib/gcc/x86_64-redhatlinux/4.4.6/../../../../include/c++/4.4.6/bits/stl_tree.h:453:
error: incomplete type ‘RAMCloud::ObjectFinder::CmpTabletKey’ not allowed
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/map:60,
我不知道为什么会这样。另外,我愿意使用std::less 实现less
【问题讨论】:
-
您不能使用前向声明来实例化
std::map。一般来说,标准库容器需要完整的类型来实例化。 -
@juanchopanza 你能举个例子吗?
-
在实例化地图之前确保
CmpTabletKey的定义可用。
标签: c++ dictionary ramcloud