Calling a function named in a variable.

This is a discussion on Calling a function named in a variable. within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by JohnAnon Yes, I know this means that I can fiddle with a switch statement in his routine ...

  1. #16
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by JohnAnon View Post
    Yes, I know this means that I can fiddle with a switch statement in his routine so that the passed value will call the correct routine of mine. But he is maintaining his routine and I'm responsible for maintaining my routines, even though they are in the same stream when compilation takes place.
    That sort of situation -- multiple developers working on different parts of the code -- is "situation normal" for ANY software project. I have yet to see somebody try to solve the problem by such ideas as "call function via string which names it." Honestly, it sounds completely insane.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  2. #17
    Registered User
    Join Date
    Mar 2011
    Posts
    19

    Smile

    Well, I've been called worse.

  3. #18
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by JohnAnon View Post
    Well, I've been called worse.
    Well, I'm not sure if it's you or your boss who's lost it.

    I guess I'd just like to hear some more detail about your problem and why this was chosen as a solution. I'm afraid we might be standing around discussing which hammer is the best choice for pounding in screws.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #19
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    At this point, it appears that the hammer almost works, so if you can bear with me for a few iterations, I would appreciate it.

    I have solved the std::map problem by adding a #include <map>.

    So I try the code:

    Code:
    //define a function pointer
    typedef double (*fnc_ptr)(double x);
    //this will be your function registry
    std::map<std::string, fnc_ptr> fncRegistry; 
    
    //maybe this is not actually needed [I try with and without this line, of course]
    double MYROUTINE_A(double x);
    
    double MYROUTINE_A(double x)
    {
       fncRegistry("MYROUTINE_A", MYROUTINE_A);  // < this line now has the error
       //define the function
    }
    
    double MYBOSSESROUTINE(const char* z)
    {
       ....
       fncRegistry(z)(10.2); //call z
       ....
    }
    Defining the function pointer survives the compiler.

    Establishing the map, aka function registry survives the compiler.

    When I add the line above the comment indicating that further code defines the function, the compiler complains with:

    error C2064: term does not evaluate to a function taking 2 arguments.

    I must admit, putting the line "fncRegistry("MYROUTINE_A", MYROUTINE_A)" *inside* the definition of the function itself puzzles me. If the purpose is to add the information to the registry, one would think you would do it once, not every time the function is called.

    In any event, that is where I'm stuck right now. I will continue to massage what I can and ask that if anybody spots the specific issue to please let me know.

    brewbuck, I appreciate your point of view, but there are very good reasons (well beyond this one project) where the name of the function [or a portion of the name of the function] will be defined in such a way as to make it subject to input from higher up the food chain (including all the way to the user), so consider this a prototype. Many text based languages have this construct built in. I'm not complaining about the fact that it isn't built-in here, just trying to re-create the capability.
    Last edited by JohnAnon; 03-28-2011 at 05:16 PM.

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,771
    >>fncRegistry("MYROUTINE_A", MYROUTINE_A);
    should be
    fncRegistry.insert( std::make_pair("MYROUTINE_A", MYROUTINE_A) );

    And should be done ONLY once. Ie, put it somewhere in your initialization code.
    It looks like someone was half-sleeping when that code was written.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    Thanks. I thought that line was misplaced and questioned its efficacy.

    I'll try your suggestion as soon as I re-install VS2008. I'm embarrassed to say that I somehow blew it up by merely trying to copy a .cpp file from one project to another. <sigh> But I did want to thank you for the response.

  7. #22
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,665
    Man that code looks like it's asking for trouble. You should work with your boss to fix it.

  8. #23
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    Well, trouble is my middle name.

    Thanks for sharing.

  9. #24
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    Adding to the map now works. Thanks for that.

    Calling the function still doesn't.

    Tried:

    fncRegistry(z)(12.08333); //call z

    and various offshoots with the word "call", but the compiler comes back with:

    error C2064: term does not evaluate to a function taking 1 arguments

    The signature of the function is:

    double MYROUTINE_A(double x)

    which, of course, is why the compiler wants my call statement to resolve to a call with one parameter.

    If anybody can spot the error, that would be appreciated.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Post the smallest and simplest program that demonstrates the problem.

    EDIT:
    My guess is that you should have written:
    Code:
    fncRegistry[z](12.08333);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    And, indeed, you would be right! Thanks!!!!!!!!!!!!!!!!!!!!!!!!

    Now we will see if I can tie all this spaghetti together.

    Thank you, thank you, thank you for that edit!!!

    Edit: And the spaghetti now works. Many thanks, again!!!!!!!!
    Last edited by JohnAnon; 04-01-2011 at 12:47 AM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers
    By MrMatt027 in forum C++ Programming
    Replies: 14
    Last Post: 12-10-2010, 03:32 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 5
    Last Post: 01-12-2006, 11:00 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21