The keyGenerator should check which data type is given as an argument and convert it to an integer. It works for float and double, but when trying to convert a string to an integer, it does not work. Do you have any ideas why this might be happening?
#include <iostream>
#include <typeinfo>
#include <string>
#include <cmath>
using namespace std;
template<typename T>
class KeyValueKnot
{
public:
T value;
int key;
KeyValueKnot *next;
KeyValueKnot(T value)
{
this->value = value;
this->key = keyGenerator(value);
this->next = NULL;
}
int keyGenerator(T value)
{
if (typeid(value) == typeid(std::string))
{
return (int) generateNumberFromString(value);
}
else if (typeid(value) == typeid(double))
{
return static_cast<int>(value);
}
else if (typeid(value) == typeid(int))
{
return value;
}
else
{
std::cout << "Unknown type" << std::endl;
}
}
int generateNumberFromString(string str)
{
int result = 0;
for (char c : str)
{
result += static_cast<int>(c);
}
return result;
}
};