简体   繁体   中英

c++ overriding default function in templated class

As a learning exercise, I wanted to make my own Hash Table class (yes, I know about std::unordered_map and std::unordered set). So, I wrote this code:

using std::cout;
using std::endl;
using std::vector;
using std::unique_ptr;

template <class K, class V, class U=std::hash<K>>
class hashTable
{
    int order=0;
    vector<myNode<K, V>> nodes;
public:
    hashTable(U u = U()){}; //  : hashPtr(u) 
    size_t gethash(K key, int level=0, const U & u=U());
};

template<class K, class V, class U>
size_t hashTable<K, V, U>::gethash(K key, int level, const U & u)
{
    return u(key) % divisors[level];
}

And it compiles fine and does what I expect when in main I have:

hashTable<int,int> hast;
for(int i=0;i<40;++i)
    cout << "hash of "<<i<<" is " << hast.gethash(i, 2) << endl;

However, when I write the following function:

size_t nodeHash(myNode<int,int> node) {
    int i = node.getkey();
    int j = node.getvalue();
    std::hash<int> hash_fn;
    return hash_fn(i)+hash_fn(j);
}

and in main I write:

hashTable < myNode<int, int>, int, nodeHash> hashMyNode;

I get the compilation error: function "nodeHash" is not a type name.

I know that I don't know what I am doing because these templated functions are new to me. I seem to know just enough to be "dangerous." However, if someone were able to nudge me in the right direction or give me a full-fledged solution to include an external function into a class (like std::unordered_map or std::sort does), I certainly would appreciate it.

Edit:

auto node = myNode<int, int>(1, 3);
hashTable < myNode<int, int>, int, size_t (*)(myNode<int,int> node)> hashMyNode;
hashMyNode.gethash(node, 2, nodeHash);

I get the following errors:

Severity    Code    Description Project File    Line    Suppression State
Error (active)  E1776   function "myNode<K, V>::myNode(const myNode<int, int> &) [with K=int, V=int]" (declared implicitly) cannot be referenced -- it is a deleted function    somePrime   E:\source\repos\somePrime\somePrime.cpp 139 

and

Severity    Code    Description Project File    Line    Suppression State
Error   C2280   'myNode<int,int>::myNode(const myNode<int,int> &)': attempting to reference a deleted function  somePrime   e:\source\repos\someprime\someprime.cpp 139 

this refers to the node variable.

You can open the std namespace and specialize the hash template for your node type as:

namespace std {
template<>
struct hash<myNode<int, int>>
{
    std::size_t operator()(myNode<int, int> const& node) const {
        int i = node.getkey();
        int j = node.getvalue();
        std::hash<int> hash_fn;
        return hash_fn(i)+hash_fn(j);
    }
};
}

and then create a table as:

hashTable < myNode<int, int>, int> hashMyNode;

now instead of using a function as a hash, you can create hash for your node as std::hash<myNode<int,int>> hash_fn{} but you don't have to explicitly create them, because you've already providing it type as default parameter (3rd parameter) of hashTable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM