When I try to call the create hash function from the following lines of code I receive the error no matching function for call
myHash.create("5", "Data");
checkTest("testSimpleIntHash #1", "Data", myHash.retrieve("5"));
The idea is that create receives the key from the KeyValuePair class and then the method would hash the key, go to that bucket, and then insert the key value pair. I believe I am receiving this error because I am not outputting to string. However, I can not figure out the syntax to properly output the key value pair to string with the create method. (I am using std::list
, and my KeyValuePair
class is functioning properly)
template<typename T> class HashTable
{
public:
static const unsigned int NUM_BUCKETS = 100000;
HashTable create(const string& key, const T& item)
{
int temp = hash(key);
arr[NUM_BUCKETS].push_back(KeyValuePair<T>(temp, item));
}
private:
int hash(const string& key) const;
//Make an array of linked lists of KeyValuePair objects.
list<KeyValuePair<T>> arr[NUM_BUCKETS];
};
template <typename T>
int HashTable<T>::hash(const string& key) const {
int temp = 0;
for (int i = key.length(); i >= 0; i--) {
temp = (13 * temp + key[i]) % NUM_BUCKETS;
}
return temp;
}
In the create
method you can return a reference to the class itself.
HashTable& create(const string& key, const T& item)
{
int temp = hash(key);
arr[NUM_BUCKETS].push_back(KeyValuePair<T>(temp, item));
return *this;
}
But anyway, you are not usign it.
And you need to declare the retrieve
method in your class HashTable
:
For example:
string retrieve(const string& input)
{
string result;
//Not sure wath do you pretend to be this method.
//But it will transform your input in your result
return result;
}