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 for 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).

Answers


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.


Need Your Help

Serially process XML data in perl

xml linux perl centos

I'm wondering which XML parser people thing would be best in my situation for Perl. I've done a lot of reading and have tried the XML::LibXML and XML::SAX. The first used up too much memory and the

Is Android's Wakelock a necessity for background services?

android android-service wakelock android-wake-lock

I am writing a long-running background service that collects and analyzes device data. To do this I start a service, the service spawns a thread and the thread does the data collection / analysis.