C++ - calling a method from a class template
I'm currently having a problem with a class template in C++. I'm currently making a hash table.
I'm using a functor as a class template to specify my hash function for each instance of a table.
IE: one table has integers 开发者_如何学Pythonfor its keys, strings for its values. Another could have strings for its keys and integers for its values, etc...
class HashString
{
public:
unsigned long operator()(std::string& key, const unsigned int tableSize)
{
// .....
}
};
template<typename keyType, typename valueType, class HashFunctor>
class HashTable
{
public:
// ....
private:
HashFunctor myHash;
};
And now let's say I want to call the method called "myHash" to hash a key, I would at first call it by doing:
myHash(key, table.size())
But gcc doesn't find a function overload for HashFuntor(string, unsigned int) for example.
Could someone tell me how I could call myHash? (Note: I would not like to change my structure of functors)
edit: This is the error message I get from my actual solution
instantiated from ‘void tp3::Table<TypeClef, TypeDonnee, FoncHachage>::insert(const TypeClef&, const TypeDonnee&) [with TypeClef = int, TypeDonnee = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, FoncHachage = tp3::HacheString]’
no match for call to ‘(tp3::HacheString) (tp3::Table<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, tp3::HacheString>::HashEntry&)’
Edit: Everywhere it says HacheString is in fact HashString (I've translated my code to paste it here).
operator() in HashString is private and is probably not const-correct. It should be a const member function taking const std::string& as its first parameter. The second parameter does not need to be const.
You seem to be calling it with HashEntry as the second parameter. What is HashEntry? It takes an unsigned int!
That might already solve some of your problems.
I assume your HacheString / HashString difference is just a typo.
精彩评论