Question

I have the following code.

file Ant.h

class hashing_func {
    public:
        unsigned long operator()(const Node& key) const {
            unsigned long hash;
            hash = key.GetId();
            return hash;
        }
};

class key_equal_fn {
    public:
        bool operator()(const Node& t1, const Node& t2) const {
            return (t1.GetId() == t2.GetId());
        }
};

class Ant {
    private:
        std :: unordered_map <Node*, int, hashing_func, key_equal_fn> nodemap;
};

However, on compiling, I keep getting the error message

[Error] no match for call to '(const hashing_func)(Node* const&)'

Obviously, my map contains Node* (node-pointers) as keys, and currently expects

long unsigned int hashing_func :: operator() ( const Node& const)

How would I go about to fix this (convert the hashing and equal function to accept Node-pointers)? Thanks a lot for the help.

Was it helpful?

Solution

The problem is that your keys are Node*, but your hash and equality comparator are for const Node&. You would need to use Node keys, or write functors for Node*:

std :: unordered_map <Node, int, hashing_func, key_equal_fn> nodemap;

or

class hashing_func 
{
    public:
        unsigned long operator()(const Node* key) const 
        {
            return = key->GetId();
        }
};

etc.

OTHER TIPS

For the hash, the signature should be

unsigned long operator()(Node* key) const;

and for the comparison, it should be

bool operator()(Node* t1, Node* t2) const;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top