Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将类公共成员函数作为模板参数传递?_C++_Class_Templates_Hashmap_Parameter Passing - Fatal编程技术网

C++ 如何将类公共成员函数作为模板参数传递?

C++ 如何将类公共成员函数作为模板参数传递?,c++,class,templates,hashmap,parameter-passing,C++,Class,Templates,Hashmap,Parameter Passing,我正在使用谷歌稀疏哈希映射库。我有以下类模板: 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>

我正在使用谷歌稀疏哈希映射库。我有以下类模板:

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;
.....

};
现在我想将
我的hashmap\u key\u类
作为
传递,
我的hashmap\u key\u类::operator()(const hashmap\u key\u类&rObj1,const hashmap\u key\u类&rObj2)
作为
EqualKey
我的hashmap\u key\u类::operator()(const hashmap\u key\u类&rObj)
as
HashFcn
to
densite\u hash\u map
class作为参数,同时在主函数中使用它作为:

main.cpp:

dense_hash_map<hashmap_key_class, int, ???????,???????> hmap;
密集散列映射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;
密集散列映射hmap;

但由于未检测到运算符,因此出现编译错误。请帮助我了解我哪里做错了。

如评论中所述,您应该将等式写成
运算符==
。另外,要么使这些运算符成为静态的,要么删除它们的一个参数(“this”指针将是相等性测试的左手操作数),否则它们将无法按预期工作

//equal comparison operator for this class
bool operator==(const hashmap_key_class &rObj2) const;

//hashing operator for this class
size_t operator()() const;
然后,您的类就可以编写如下客户机代码:

my_hashmap_key_class a = ...;
my_hashmap_key_class b = ...;

a == b;   // calls a.operator==(b), i.e. compares a and b for equality
a();      // calls a.operator()(), i.e. computes the hash of a

然后,使用默认模板参数就可以了。

我想您希望这些操作符是
静态的。另外,为什么不使用
操作符==
进行等式比较呢?(您仍然需要手动定义它)使操作符
静态
。或者使用一个相当奇怪但并非闻所未闻的事实,即您的类是它自己的相等函子,只需传递
my\u hashmap\u key\u class
。请注意,将构造一个实例来执行比较,并且两个参数都不是构造的比较器本身。
HashFn
参数也是如此。我不经常看到它,但它仍然可以工作,因为您已经定义了这段代码。请注意,您的类必须支持默认构造(我假设它支持)<代码>密集散列映射hmap
应该可以工作。密集散列映射hmap可以工作。。。。。。。。。但给出编译器警告,如------警告:在添加的符号文件系统提供的DSO 0x7ffff7ffa000中找不到可加载的节警告:未找到用于…的RTTI符号继续这是真的…但我对对象实例化或成员函数定义的方式不感兴趣(因为google稀疏散列代码需要我们的类采用特定的格式)。但是如何将成员函数用作模板参数并加以利用。“那么,使用默认模板参数就可以了。”是我需要知道的部分&如何实现它意味着省略参数:
densite\u hash\u map
应该做,但我不能测试它。请报告这是否有问题。它给出的编译错误是:未定义对`std::tr1::hash::operator()(my\u hashmap\u key\u class)const'的引用。在这种情况下,请尝试
densite\u hash\u map
my_hashmap_key_class a = ...;
my_hashmap_key_class b = ...;

a == b;   // calls a.operator==(b), i.e. compares a and b for equality
a();      // calls a.operator()(), i.e. computes the hash of a