RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 1106 | Replies: 3 | Thread Tools  Display Modes
Reply
Join Date: Jun 2007
Posts: 1
Reputation: sowmi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sowmi sowmi is offline Offline
Newbie Poster

Reg Function pointers

  #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,


command.h
**********
#define FUNCPTR  void *
#define MAX_NUM_COMMANDS 3

typedef struct
{
   char command[LINELEN];                           /* name of the command */
   char parm1[LINELEN];                             /*parameter 1 */
   char parm2[LINELEN];                             /*parameter 2 */
                                      
} ExeCmd_s;
class FunctionEntry
{

public:
    char* commandString;
    FUNCPTR pFun;   
    int SearchCommand(char* command);
};
FunctionEntry funArr[MAX_NUM_COMMANDS]=
{
	    {"aaa",(FUNCPTR)aaa},
            {"bbb", (FUNCPTR)bbb},
   	    {"ccc",(FUNCPTR)ccc}

};
enum commandName
{
	aaa = 0,
	bbb
};


command.c
**********
void void Execute ()
{
  .........
  .........
 Execmd execmd;
 FunctionEntry command;
 int rc;
 if(0 != SearchCommand(exeCmd.command))
 {
    command = FunctionEntry.SearchCommand(exeCmd.command);
    switch((int)command.numOfArguements)
    {
         case Zero :
           rc = ((command.pFun)();
           break;
	 case one:
	   rc = ((command.pFun),exeCmd.parm1);
	   break;
    }
 }
 else
 {
   printf("No match");
 }

}



//Function to search and returns the index of the command

int FunctionEntry :: SearchCommand(char* command)
{
     
     ExeCmd_s exeCmd;  //already had a Structure where I am extracting the commandname..
     int index = 0;     
     while(index < MAX_NUM_COMMANDS)
     {
	 if(strcmp(exeCmd.command,funArr[index]) == 0)
	 {
	    return &funArr[index];
	 }
         index++;	
    }
     return NULL;
}




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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Posts: 2,814
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 16
Solved Threads: 240
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Reg Function pointers

  #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.
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Join Date: Oct 2006
Location: NY
Posts: 218
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Rep Power: 4
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: Reg Function pointers

  #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  
Join Date: Dec 2006
Location: india
Posts: 1,087
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 
Rep Power: 10
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Reg Function pointers

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 10:52 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC