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 6973845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:09:39+00:00 2026-05-27T17:09:39+00:00

I am having trouble passing a function as an argument to another function. I

  • 0

I am having trouble passing a function as an argument to another function. I know this has been addressed already here, but I would appreciate a little more help because I’m having no success. I have tried to adapt this example to my needs:

C++:How to pass reference-to-function into another function?

I’m trying to make my code more readable. I have a huge amount of code and a lot of it is repetitive. For example:

CreateDatabaseEntry( A_key, name_vector, name_vector2, dimension, "_mySuffix" );
CreateDatabaseEntry( B_key, name_vector, name_vector2, dimension, "_mySuffix" ); ...

Let’s say I have 26 of these calls in my current main file, one each for B_key, C_key, etc. I would like to move this kind of stuff to functions in a separate “helper” file (helper.cpp/h). For example, into CreateDatabaseEntries().

Problem

Here’s my problem. I would have to pass CreateDatabaseEntry into CreateDatabaseEntries. Each call to CreateDatabaseEntry takes different arguments (for example, A_key, then B_key, and so on). I have tried using the example at the link I provided above. But I get “no matching function call” errors. My guess is that I would not get errors if I were calling ONLY one CreateDatabaseEntry. Because I could hard-code “A_key” in the definition of CreateDatabaseEntries. The problem seems to be that I can’t generalize the definition of CreateDatabaseEntries to take A_key, B_key, C_key, or whatever.

Also, this is just a simple representation. In practice, I would want to create several “umbrella” functions like CreateDatabaseEntries that would take not only arbitrary A_key, B_key,…, but also arbitrary name_vector and other arguments.

I am a rookie and it is possible that I’m totally barking up the wrong tree. If my explanation makes any sense to anyone, maybe there is a completely different way I could accomplish this? If so, it would be great to know about it. Thanks.

  • 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-27T17:09:40+00:00Added an answer on May 27, 2026 at 5:09 pm

    Sending the functions seems strange. Parameter passing is indeed better as m0skit0 said.

    Why don’t you just create an array of parameters, then use a loop to pass each line ?
    Should be simpler and you don’t need to worry about pointers or memory allocation (depending on the parameters and inner mechanics from the function , of course).

    imagine this:

    int paramList [5] = { 0, 2, 4, 6, 8 };
    
    for (i = 0; i < 5; ++ i)
        myFunc(paramList[i]);
    

    Well obviously in your case you’d make pairs if you have more than 1 parameter… but you get the idea, I think.

    If at any point you decide to change the parameter type, you could just use polymorphism on the parameters (or switch to templates), unless you want to keep it all C.

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

Sidebar

Related Questions

having some trouble passing a function as a parameter of another function... ERROR: Error
I'm having trouble passing a C++0x lambda function as the second argument to makecontext
I am grabbing divs using the .each function, but I am having trouble passing
I'm having some trouble passing a number as an argument for a method: -
CakePHP Newbie :) I am having trouble accessing another controller and passing that data
I'm having trouble passing a big array to a function in C. I declare:
I'm having trouble passing a structure array as a parameter of a function struct
I am having trouble passing data retrieved from a $.post() function to use in
Ok, so I'm using Jquery's AJAX function and it's having trouble passing a URL
I'm having trouble passing a pointer to a structure as an argument to a

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.