Let me put it step by step. The problem is I am not able to instantiate a Template class. Pls help where am i doing wrong.
I have a template class as below :
template <typename K, typename V>
class HashNodeDefaultPrint {
public:
void operator()(K key, V value) {}
};
Then I have a specific class that belongs to the above template for K = int, V = int
class HashNodePrintIntInt {
public:
void operator()(int key, int value) {
printf ("k = %d, v = %d", key, value);
}
};
Similarly, I have another template class as below :
template <typename K>
class defaultHashFunction {
public:
int operator()(K key) {
return 0;
}
};
Again, I have a specific class which matches the above template for K = int
class HashFunctionInteger {
public:
int operator()(int key) {
return key % TABLE_SIZE;
}
};
Now I create a HashNode templated Class as below :
template <typename K, typename V, typename F = HashNodeDefaultPrint<K, V>>
class HashNode {
public:
K key;
V value;
F printFunc;
HashNode *next;
HashNode(K key, V value, F func) {
this->key = key;
this->value = value;
next = NULL;
}
};
And finally a HashMap Templates class as Below :
template <typename K, typename V, typename F = defaultHashFunction<K>, typename F2 = HashNodeDefaultPrint<K,V>>
class HashMap {
private:
HashNode<K, V, F2> *table_ptr;
F hashfunc;
public:
HashMap() {
table_ptr = new HashNode<K,V, F2> [TABLE_SIZE]();
}
};
Now in main(), i am instantiating the HashMap class as below :
int
main(int argc, char **argv) {
HashMap<int, int, HashFunctionInteger, HashNodePrintIntInt> hmap;
return 0;
}
but i am seeing compilation error :
vm@ubuntu:~/src/Cplusplus/HashMap$ g++ -g -c hashmap.cpp -o hashmap.o
hashmap.cpp: In instantiation of ‘HashMap<K, V, F, F2>::HashMap() [with K = int; V = int; F = HashFunctionInteger; F2 = HashNodePrintIntInt]’:
hashmap.cpp:157:65: required from here
hashmap.cpp:107:21: error: no matching function for call to ‘HashNode<int, int, HashNodePrintIntInt>::HashNode()’
107 | table_ptr = new HashNode<K,V, F2> [TABLE_SIZE]();
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
hashmap.cpp:30:9: note: candidate: ‘HashNode<K, V, F>::HashNode(K, V, F) [with K = int; V = int; F = HashNodePrintIntInt]’
30 | HashNode(K key, V value, F func) {
| ^~~~~~~~
hashmap.cpp:30:9: note: candidate expects 3 arguments, 0 provided
hashmap.cpp:27:7: note: candidate: ‘constexpr HashNode<int, int, HashNodePrintIntInt>::HashNode(const HashNode<int, int, HashNodePrintIntInt>&)’
27 | class HashNode {
| ^~~~~~~~
hashmap.cpp:27:7: note: candidate expects 1 argument, 0 provided
hashmap.cpp:27:7: note: candidate: ‘constexpr HashNode<int, int, HashNodePrintIntInt>::HashNode(HashNode<int, int, HashNodePrintIntInt>&&)’
hashmap.cpp:27:7: note: candidate expects 1 argument, 0 provided
vm@ubuntu:~/src/Cplusplus/HashMap$
What is wrong with this instantiation when I am correctly specifying the specific class names to define template variables?