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.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.Below are my code,Belowis my code. Code is singular in programming.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
I see no definition for public function
int SearchCommand()
in class FunctionEntry.
I suppose that might stop you dead in your tracks!
JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
...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).
#include <iostream>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <map>
using namespace std ;
using namespace boost ;
struct function_one // function object
{
void operator() ( const string& arg ) const
{ cout << "function_one( " << arg << " )\n" ; }
};
void function_two( const string& arg ) // free function
{ cout << "function_two( " << arg << " )\n" ; }
struct object
{
explicit object( int ii = 0 ) : i(0) {}
int i ;
void function_three( const string& arg ) // member function
{ cout << "object::function_three( " << arg << " )\n" ; ++i ; }
};
int main()
{
typedef function< void ( const string& ) > function_t ;
typedef pair< function_t, string > fn_with_arg_t ;
map< string, fn_with_arg_t > function_map ;
function_map[ "function object" ] = fn_with_arg_t( function_one(), "one" );
function_map[ "free function" ] = fn_with_arg_t( function_two, "two" ) ;
object obj ;
function_map[ "member function" ] =
fn_with_arg_t( bind( &object::function_three, ref(obj), _1 ), "three" );
string key ;
while( getline( cin, key ) )
{
typedef map< string, fn_with_arg_t >::iterator iterator ;
iterator ptr = function_map.find(key) ;
if( ptr != function_map.end() ) ptr->second.first( ptr->second.second ) ;
else cout << "function not found\n" ;
}
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287