943,816 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1773
  • C++ RSS
Jun 27th, 2007
0

Reg Function pointers

Expand Post »
Hi All,

I am trying to implement the concept of function pointer in C++.
While compiling i got some errors which I couldnt figure out the reason .
So,Please let me know what changes should be done for resolving that error.

I am trying to execute one particular command function when the commandis pressed.Likewise, I have 100 commands and whenever the commands are pressed the corresponding function + arguements should be passed and the function pointer should execute the command.


Below are my code,


C++ Syntax (Toggle Plain Text)
  1. command.h
  2. **********
  3. #define FUNCPTR void *
  4. #define MAX_NUM_COMMANDS 3
  5.  
  6. typedef struct
  7. {
  8. char command[LINELEN]; /* name of the command */
  9. char parm1[LINELEN]; /*parameter 1 */
  10. char parm2[LINELEN]; /*parameter 2 */
  11.  
  12. } ExeCmd_s;
  13. class FunctionEntry
  14. {
  15.  
  16. public:
  17. char* commandString;
  18. FUNCPTR pFun;
  19. int SearchCommand(char* command);
  20. };
  21. FunctionEntry funArr[MAX_NUM_COMMANDS]=
  22. {
  23. {"aaa",(FUNCPTR)aaa},
  24. {"bbb", (FUNCPTR)bbb},
  25. {"ccc",(FUNCPTR)ccc}
  26.  
  27. };
  28. enum commandName
  29. {
  30. aaa = 0,
  31. bbb
  32. };
  33.  
  34.  
  35. command.c
  36. **********
  37. void void Execute ()
  38. {
  39. .........
  40. .........
  41. Execmd execmd;
  42. FunctionEntry command;
  43. int rc;
  44. if(0 != SearchCommand(exeCmd.command))
  45. {
  46. command = FunctionEntry.SearchCommand(exeCmd.command);
  47. switch((int)command.numOfArguements)
  48. {
  49. case Zero :
  50. rc = ((command.pFun)();
  51. break;
  52. case one:
  53. rc = ((command.pFun),exeCmd.parm1);
  54. break;
  55. }
  56. }
  57. else
  58. {
  59. printf("No match");
  60. }
  61.  
  62. }
  63.  
  64.  
  65.  
  66. //Function to search and returns the index of the command
  67.  
  68. int FunctionEntry :: SearchCommand(char* command)
  69. {
  70.  
  71. ExeCmd_s exeCmd; //already had a Structure where I am extracting the commandname..
  72. int index = 0;
  73. while(index < MAX_NUM_COMMANDS)
  74. {
  75. if(strcmp(exeCmd.command,funArr[index]) == 0)
  76. {
  77. return &funArr[index];
  78. }
  79. index++;
  80. }
  81. return NULL;
  82. }




Please let me know where i should change the code.
Expecting some solutions for this code ASAP .

Thanks,
Sowmi
Last edited by WaltP; Jun 27th, 2007 at 2:42 pm. Reason: Use the PREVIEW button to see if your post looks correct.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sowmi is offline Offline
1 posts
since Jun 2007
Jun 27th, 2007
0

Re: Reg Function pointers

Click to Expand / Collapse  Quote originally posted by sowmi ...
Hi All,

I am trying to implement the concept of function pointer in C++.
While compiling i got some errors which I couldnt figure out the reason .
So,Please let me know what changes should be done for resolving that error.
Which errors? Without posting them, we have no idea what the errors are.


Click to Expand / Collapse  Quote originally posted by sowmi ...
I am trying to execute one particular command function when the commandis pressed.Likewise, I have 100 commands and whenever the commands are pressed the corresponding function + arguements should be passed and the function pointer should execute the command.
Without a clean compile, this info is moot.


Click to Expand / Collapse  Quote originally posted by sowmi ...
Below are my code,
Below is my code. Code is singular in programming.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Jun 27th, 2007
0

Re: Reg Function pointers

I see no definition for public function
int SearchCommand()
in class FunctionEntry.

I suppose that might stop you dead in your tracks!
JRM
Reputation Points: 130
Solved Threads: 75
Practically a Master Poster
JRM is offline Offline
618 posts
since Oct 2006
Jun 28th, 2007
0

Re: Reg Function pointers

Click to Expand / Collapse  Quote originally posted by sowmi ...
...I have 100 commands and whenever the commands are pressed the corresponding function + arguements...
it is much more flexible to represent functions as objects . this would allow free functions, function objects and member functions to be treated polymorphically. also, since these are objects, they can support operations (eg.bind). in the following example, boost.function and boost.bind are used (from the boost libraries); however, both function and bind would be part of c++0x (they are in tr1).
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <boost/function.hpp>
  3. #include <boost/bind.hpp>
  4. #include <map>
  5. using namespace std ;
  6. using namespace boost ;
  7.  
  8. struct function_one // function object
  9. {
  10. void operator() ( const string& arg ) const
  11. { cout << "function_one( " << arg << " )\n" ; }
  12. };
  13.  
  14. void function_two( const string& arg ) // free function
  15. { cout << "function_two( " << arg << " )\n" ; }
  16.  
  17. struct object
  18. {
  19. explicit object( int ii = 0 ) : i(0) {}
  20. int i ;
  21. void function_three( const string& arg ) // member function
  22. { cout << "object::function_three( " << arg << " )\n" ; ++i ; }
  23. };
  24.  
  25. int main()
  26. {
  27. typedef function< void ( const string& ) > function_t ;
  28. typedef pair< function_t, string > fn_with_arg_t ;
  29. map< string, fn_with_arg_t > function_map ;
  30. function_map[ "function object" ] = fn_with_arg_t( function_one(), "one" );
  31. function_map[ "free function" ] = fn_with_arg_t( function_two, "two" ) ;
  32. object obj ;
  33. function_map[ "member function" ] =
  34. fn_with_arg_t( bind( &object::function_three, ref(obj), _1 ), "three" );
  35. string key ;
  36. while( getline( cin, key ) )
  37. {
  38. typedef map< string, fn_with_arg_t >::iterator iterator ;
  39. iterator ptr = function_map.find(key) ;
  40. if( ptr != function_map.end() ) ptr->second.first( ptr->second.second ) ;
  41. else cout << "function not found\n" ;
  42. }
  43. }
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Integer Stack Copy Function
Next Thread in C++ Forum Timeline: which process reads the cin





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC