Reg Function pointers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2007
Posts: 1
Reputation: sowmi is an unknown quantity at this point 
Solved Threads: 0
sowmi sowmi is offline Offline
Newbie Poster

Reg Function pointers

 
0
  #1
Jun 27th, 2007
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,


  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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Reg Function pointers

 
0
  #2
Jun 27th, 2007
Originally Posted by sowmi View 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.
Which errors? Without posting them, we have no idea what the errors are.


Originally Posted by sowmi View Post
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.


Originally Posted by sowmi View Post
Below are my code,
Below is my code. Code is singular in programming.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 222
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: Reg Function pointers

 
0
  #3
Jun 27th, 2007
I see no definition for public function
int SearchCommand()
in class FunctionEntry.

I suppose that might stop you dead in your tracks!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Reg Function pointers

 
0
  #4
Jun 28th, 2007
Originally Posted by sowmi View Post
...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).
  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. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC