【发布时间】:2013-11-06 13:04:50
【问题描述】:
我正在使用 google 稀疏哈希图库。我有以下类模板:
template <class Key, class T,
class HashFcn = std::tr1::hash<Key>,
class EqualKey = std::equal_to<Key>,
class Alloc = libc_allocator_with_realloc<std::pair<const Key, T> > >
class dense_hash_map {
.....
typedef dense_hashtable<std::pair<const Key, T>, Key, HashFcn, SelectKey,
SetKey, EqualKey, Alloc> ht;
.....
};
现在我将自己的类定义为:
class my_hashmap_key_class {
private:
unsigned char *pData;
int data_length;
public:
// Constructors,Destructor,Getters & Setters
//equal comparison operator for this class
bool operator()(const hashmap_key_class &rObj1, const hashmap_key_class &rObj2) const;
//hashing operator for this class
size_t operator()(const hashmap_key_class &rObj) const;
};
现在我想将my_hashmap_key_class 作为Key、my_hashmap_key_class::operator()(const hashmap_key_class &rObj1, const hashmap_key_class &rObj2) 作为EqualKey 和my_hashmap_key_class::operator()(const hashmap_key_class &rObj) 作为HashFcn 传递给dense_hash_map 类作为参数,同时在主函数中使用它作为:
main.cpp:
dense_hash_map<hashmap_key_class, int, ???????,???????> hmap;
将类成员函数作为模板参数传递的正确方法是什么?
我试过像这样传递:
dense_hash_map<hashmap_key_class, int, hashmap_key_class::operator(const hashmap_key_class &rObj1, const hashmap_key_class &rObj2),hashmap_key_class::operator()(const hashmap_key_class &rObj)> hmap;
但由于未检测到运算符,因此出现编译错误。请帮助我意识到我做错了什么。
【问题讨论】:
-
我猜你希望这些运算符是
static。另外,为什么不使用operator==进行相等比较? (你还是要手动定义) -
让运营商
static. -
或者使用相当奇怪但并非闻所未闻的事实,即您的类是它自己的相等函子,并简单地传递
my_hashmap_key_class。请注意,将构造一个实例来执行比较,并且两个参数都不是构造的比较器本身。HashFn参数也是如此。我不经常看到它,但它应该仍然可以在您定义此代码时工作。请注意,您的类必须支持默认构造(我假设它支持)。 -
即
dense_hash_map<my_hashmap_key_class, int, my_hashmap_key_class,my_hashmap_key_class> hmap应该可以工作。 -
dense_hash_map
hmap 工作.........但给编译器警告 -----警告:在添加的符号文件中找不到可加载部分系统在 0x7ffff7ffa000 处提供的 DSO 警告:RTTI 符号未找到 ....继续
标签: c++ class templates hashmap parameter-passing