0

私はグーグルスパースハッシュマップライブラリを扱っています。そして、次のクラス テンプレートがあります。

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_classas a Keymy_hashmap_key_class::operator()(const hashmap_key_class &rObj1, const hashmap_key_class &rObj2)as EqualKey、およびmy_hashmap_key_class::operator()(const hashmap_key_class &rObj)asHashFcnをパラメーターとしてクラスに渡したいと思います。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;

しかし、演算子が検出されないため、コンパイルエラーが発生します。私が間違っているところを理解するのを手伝ってください。

4

1 に答える 1

0

コメントで説明されているように、等式は として記述する必要がありますoperator==。また、これらの演算子を静的にするか、それらのパラメーターの 1 つを削除します ("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

次に、デフォルトのテンプレート パラメーターを使用して問題ないはずです。

于 2013-10-27T20:53:30.427 に答える