c is not able to create a template class

You think much to complicated 🙂

If you already use templates, you can specialize them if you need. That did NOT require new class names!

Starting with:

template <typename K, typename V>
struct HashNodePrint { 
    void operator()(K key, V value) {} 
};

// and specialize for int,int:
template<>
struct HashNodePrint<int,int> {
    void operator()(int key, int value) {
        printf ("k = %d, v = %d", key, value);
    }
};

The same for

template <typename K>
struct HashFunction {
    int operator()(K key) {
        return 0;
    }
};

template <>
struct HashFunction<int> {
    int operator()(int ) {
        return 0;
    }
};

And I expect you do not longer need the template default funcs, as you have not longer the need to manually specify the specialized functions. They are now automatically mapped to the specialized version.

This will end up here:

template <typename K, typename V>
class HashNode {

    public:
        K key;
        V value;
        HashFunction<K> printFunc;
        HashNode *next;
        HashNode(K key, V value) {
            this->key = key;
            this->value = value;
            next = NULL;
        }
        HashNode(){}
};

But still you have no default constructor for your HashNode. Maybe you generate a default one OR you put some default values in the call or pass the values down from construction at all. I have no idea what you want to achieve by generating it in a defaulted way.

template <typename K, typename V >
class HashMap {
    private:
        HashNode<K, V> *table_ptr;
        HashFunction<K> hashfunc;
    public:
        HashMap(/* put maybe a container type here */) {
            // no idea how big your TABLE_SIZE will be. 
            // 
            table_ptr = new HashNode<K,V> [TABLE_SIZE]{ /* and forward as needed */};
        }
};

int main() {

    HashMap<int, int> hmap{ /* pass args maybe in container or std::initializer_list */;
    return 0;
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top