我正在尝试实现一个自定义 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
的实现