CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Feb 2010
    Posts
    28

    Having trouble with a function object and templates

    I have two classes defined as the following, with each in two header files (content in bold is what I'm having an issue with):

    Code:
    class StringHashKey
    {
      public:
    
      template<class HF>     
      int operator()(const string&); // problem here!!!!
       // converts a string parameter into an integer
    
      //...other public methods
    };
    
    // StringHashKey.h
    
    //-------------------------------------------
    template <class T, class HF>
    class HashTable
    {
    private:
    
       int num_Buckets;
        // # of pointers to linked lists
        // in the hash table
    
       int bucketsUsedCount;
        // # of data items currently stored in
        // the hash table
    
        HF convertKEY;   <--- problem here!!
        // function object which will be used
        // to convert a data item to an integer
        // hash key
    
    // public methods...
    };
    // HashTable.h
    In the main program, main.cpp, I've declared a hash table as so:

    Code:
    HashTable<string, StringHash> dictionary(BUCKET_COUNT);
    The program I'm having is when I try to use the function object, I get an error saying there is no matching call

    i.e.

    Code:
    template<class T, class HF>
    bool HashTable<T, HF>::insert(const T &item)
    {
      int HashKey = convertKEY(item);  <--- this is where the error occurs
    ......
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Having trouble with a function object and templates

    Since type HF isn't used in the signature of operator(), there is no way for the compiler to automatically deduce what it should be. You'll need to specify that explicitly in the call. I'm not actually sure what that would look like with operator(), actually......although I'm not actually sure why you're templatizing that call in the first place.

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Having trouble with a function object and templates

    Code:
    template<class T>
    stuct StringHashKey
    {
        int operator()(const T&); // problem here!!!!
        // converts a string parameter into an integer
    
        //...NO other public methods
    };
    This is how function objects are usually implemented. Your code did not work because StringHashKey was not a template, and operator() did not take a HF object, but a string.

    That said, are you really trying to implement a hashTable? I think it is probably one of the hardest things you could try to do yourself. But if you are feeling bold...
    Last edited by monarch_dodra; April 19th, 2010 at 08:23 PM.

  4. #4
    Join Date
    Feb 2010
    Posts
    28

    Re: Having trouble with a function object and templates

    Quote Originally Posted by monarch_dodra View Post
    Code:
    template<class HF>
    stuct StringHashKey
    {
        int operator()(const HF&); // problem here!!!!
        // converts a string parameter into an integer
    
        //...NO other public methods
    };
    Why did you change the parameter in the operator() from string to HF? I sort of need that to change to an integer so it can be returned. Unless, there is something I'm missing? :[

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Having trouble with a function object and templates

    I don't understand what HF is supposed to be in the first place.

    The approach used by Boost and TR1 to specifying a hash function is simply to put a function called hash() in the same namespace as the type it operates on. Then, argument-dependent lookup will simply find the correct hash function.

  6. #6
    Join Date
    Feb 2010
    Posts
    28

    Re: Having trouble with a function object and templates

    Quote Originally Posted by Lindley View Post
    I don't understand what HF is supposed to be in the first place.
    When you have multiple templates that are in different classes, aren't you suppose to give them different names? Like one occurance there is a T and an HF?

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Having trouble with a function object and templates

    I don't know what you mean. Here's my point of confusion:
    Code:
      template<class HF>     
      int operator()(const string&); // problem here!!!!
    I don't know what role the class HF is supposed to play in this function. It certainly doesn't relate in any way to the argument or the return type. Maybe something internal?

    Code:
    HF convertKEY;
    Okay, convertKey is an HF object. While it's a different template so it doesn't necessarily need to be the same type as above, it's reasonable to suppose that it's intended to be.

    Code:
    bool HashTable<T, HF>::insert(const T &item)
    {
      int HashKey = convertKEY(item);  <--- this is where the error occurs
    Okay, so clearly the class HF is a functor or function pointer taking a T and returning something convertible to an int. But this means that the type of HF either needs to be dependent on T, which is not shown above, or else the class instantiating HF must be related to the class instantiating T. Okay, fair enough.

    I still don't see where a StringHashKey fits into the picture here, though, or why a StringHashKey would need something taking a T as an argument when StringHashKey doesn't know about class T.

  8. #8
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Having trouble with a function object and templates

    Quote Originally Posted by yellowMonkeyxx View Post
    When you have multiple templates that are in different classes, aren't you suppose to give them different names? Like one occurance there is a T and an HF?
    No. You can use T everywhere. As a matter of fact, it is a sort of convention that people agree to and understand pretty easily.

    Quote Originally Posted by yellowMonkeyxx View Post
    Why did you change the parameter in the operator() from string to HF? I sort of need that to change to an integer so it can be returned. Unless, there is something I'm missing? :[
    I was under the impression you wanted StringHashKey to be a template that could take any parameter. If this is not the case, than a simple:

    Code:
    struct StringHashKey
    {
        int operator()(const string&);
    };
    Will suffice.

  9. #9
    Join Date
    Feb 2010
    Posts
    28

    Re: Having trouble with a function object and templates

    Code:
    HashTable.h: In member function &#226;bool HashTable<T, HF>::insert(const T&) 
    [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, HF = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]&#226;:
    main.cpp:50:   instantiated from here
    HashTable.h:217: error: no match for call to &#226;(std::basic_string<char, std::char_traits<char>, std::allocator<char> >) (const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)&#226;
    Not sure if this will help, but here is the error code I keep getting.

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Having trouble with a function object and templates

    Well no, of course there's no such function; you only declared a function template, you haven't instantiated it with anything.

    The greater problem here is that I don't understand what you are intending to do, so therefore I can't say what you need to change in order to do it. You need to explain your intentions at a high level, but not so high a level that I can't relate it back to what you've written. A good start would be describing what each of those template parameters is intended to be.

  11. #11
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Having trouble with a function object and templates

    Quote Originally Posted by yellowMonkeyxx View Post
    Code:
    HashTable.h: In member function âbool HashTable<T, HF>::insert(const T&) 
    [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, HF = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]â:
    main.cpp:50:   instantiated from here
    HashTable.h:217: error: no match for call to â(std::basic_string<char, std::char_traits<char>, std::allocator<char> >) (const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)â
    Not sure if this will help, but here is the error code I keep getting.
    Probably because you wrote HF=string instead of HF = StringHashKey.

    I need to see the declarations of:
    - StringHashKey
    - HashTable
    - Most importantly, the instantiation of your template.

    PS: Is StringHash == StringHashKey? I know exactly where your problem is, but it is hard to explain with no support to show.

  12. #12
    Join Date
    Feb 2010
    Posts
    28

    Re: Having trouble with a function object and templates

    Quote Originally Posted by monarch_dodra View Post
    Probably because you wrote HF=string instead of HF = StringHashKey.

    I need to see the declarations of:
    - StringHashKey
    - HashTable
    - Most importantly, the instantiation of your template.

    PS: Is StringHash == StringHashKey? I know exactly where your problem is, but it is hard to explain with no support to show.
    That was a typo, I already fixed that in the main program.

    If you want to see the whole program well here it is:
    http://faculty.cs.niu.edu/~freedman/340/hw5/hw5.html

  13. #13
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Having trouble with a function object and templates

    Quote Originally Posted by yellowMonkeyxx View Post
    That was a typo, I already fixed that in the main program.

    If you want to see the whole program well here it is:
    http://faculty.cs.niu.edu/~freedman/340/hw5/hw5.html
    I mean the code you wrote...

  14. #14
    Join Date
    Feb 2010
    Posts
    28

    Re: Having trouble with a function object and templates

    Quote Originally Posted by monarch_dodra View Post
    I mean the code you wrote...
    Which code of mine do you need to see? Do you want to see it for both classes?

  15. #15
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Having trouble with a function object and templates

    I can explain the error:

    Since "std::basic_string<char, std::char_traits<char>, std::allocator<char> >" is typedef by "string", the above error translates to:

    Code:
    HashTable.h: In member function âbool HashTable<T, HF>::insert(const T&) 
    [with T = string, HF = string]:
    main.cpp:50:   instantiated from here
    HashTable.h:217: error: no match for call to (string) (const string&)
    What this means, is that at one point or another, you probably got HF and T confused, and something that looks like "Hasher(const value&)" is actually translated into "string(const string&)"

    I was hoping I could show you where this confusion stemmed from in your code. I can't guess what you write, and believe me, thousands of things can go wrong.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured