Is it possible to have pointer to unordered_set<MyClass> as a part of myClass?

308 views Asked by At

My code looks like that:

class Node : public bitset<BITS_COUNT> {
public:
    Node(string s) : bitset<BITS_COUNT>(s) {}
    void setGroup(unordered_set<Node>* newSet) { currentGroup = newSet; }
    unordered_set<Node>* getCurrentSet() { return currentGroup; }
private:
    unordered_set<Node>* currentGroup = nullptr;
};

But complier doesn't allow me to do this, since there is no hash function defined for class Node. I'd like it to use hash function from base class, so I did this:

namespace std
{
    template<>
    struct hash<Node>
    {
        size_t operator()(const Node& k) const
        {
            return k.hash();
        }
    };
}

But it still doesn't work. If I put this before Node delcaration, k.hash() is undefined (and I can't forward declare Node: public bitset<> since it isn't possible). If I put this after class declaration I get error that there is no hash function for class Node.

How can I solve this problem?

1

There are 1 answers

2
S. Kaczor On

Thanks Frank, your comment is actually a solution for me. If someone needs the code it looks like this:

namespace std
{
    template<>
    struct hash<Node>
    {
        size_t operator()(const Node& k) const;
    };
}

class Node : public std::bitset<BITS_COUNT> {
public:
    Node(std::string s) : bitset<BITS_COUNT>(s) {}
    void setGroup(std::unordered_set<Node>* newSet) { currentGroup = newSet; }
    std::unordered_set<Node>* getCurrentSet() { return currentGroup; }
private:
    std::unordered_set<Node>* currentGroup = nullptr;
};

namespace std
{
    size_t hash<Node>::operator()(const Node& k) const
    {
        return k.hash();
    }
}