Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7569307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:07:59+00:00 2026-05-30T15:07:59+00:00

I have a template class that I have made called hash . My template

  • 0

I have a template class that I have made called hash. My template class hash takes three non-type parameters. The definition of the hash class is below:

template <typename array_type, typename ptr_to_hash, typename hash_type>
class hash
{
    public:
    //default constructor
    hash();

    /* Overloaded Constructors */

    // instantiates a hash object and the pointer to the hash_function

    hash(const int&, std::ifstream&, const char*, ptr_to_hash*);


    /* Methods for Hash Class */
    void insert_to_hash();

    // some other stuff    
};

As you can see I want my non-type parameter ptr_to_hash to be a pointer to my function void insert_to_hash. The implementation of the above overloaded contructor looks like:

template <typename array_type, typename ptr_to_hash, typename hash_type>
hash<array_type, ptr_to_hash, hash_type>::hash(const int& dim, std::ifstream& in, const char* file, ptr_to_hash* hash_ptr)
{
    // do some stuff to allocate from file

    // point function pointer to correct function
    hash_ptr = &this->insert_to_hash();
}

Now in main I am attempting to create a pointer to my hash function. So I first create a void function pointer and then pass that to my overloaded constructor:

int main()
{
    // create void function pointer
    void (*foo)();

    //create hash obj. from data read in from argv[1]
    hash< member<int>, void(*), member<int> > awesome( count_lines(in,file), in, file, foo);
}

In the above member<int> is a template struct and count_lines() just returns an integer value for the amount lines in the file. When I attempt to do this I get the error

no matching function for call to ‘hash<member<int>, void*, member<int> >::hash(int, std::ifstream&, const char*&, void (*&)())

When I look at the error above I seem to be passing my foo function pointer object as *& which of course does not match any function calls in my class.

That is the crux of my problem. I am unsure of how to pass a function pointer that points to my void insert_to_hash() in my hash class when using templates. I am clearly doing it wrong.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:08:00+00:00Added an answer on May 30, 2026 at 3:08 pm

    The type of foo as a reference is void(&)(), and as a pointer it is void(*)(). I would omit the explicit pointer from the template signature and let it be part of the argument:

    template <typename FPtr> void run(FPtr f) { f(); }
    
    void foo() { /* ... */ }
    
    // ...
    
    run<void(*)()>(foo);
    

    You could also declare run(FRef * f) { f(); } and say run<void(&)()>(foo), but I wouldn’t bother. There’s no way to not have the pointer in a function pointer, so you might as well absorb it into the argument type itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C++ template class that gets instantiated with 3 different type parameters.
I have a template class that is only valid for couple of template parameters:
I have the following use case: There's a class called Template and with that
I have a recursive template definition (I just made up that term). I think
I have a template class that I serialize (call it C), for which I
I want to have a template class that looks something like what I have
I have a template class where I want to use objects of that class
I have code that looks like this: template<class T> class list { public: class
I have a template class like below. template<int S> class A { private: char
I have made a template class for a node in a linked list and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.