|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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, 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 WingedPanther; 06-27-2007 at 10:58 AM. |
| Sponsored Links |
|
|
|
|||||
|
Issues with the code:
1) you are using #defines instead of consts 2) you didn't give us the errors 3) you have a function defined as type "void void" 4) I don't see (could be blind) where you are using your function pointer.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
|
|||||
|
You've got the whole idea about function pointers wrong.
void * isn't a function pointer, it's simply just a pointer to an unknown type. Function pointers is much more complicated, if we can say it in that way. When working with function pointers you need to know three things. First of all you need to know the return-type of the function, you need to point to. Second, you need to know how many parameters the function, you're trying to point at accepts. And third, you need to know exactly which of the types the parameter list accepts. If we says that the function "write", has a return-type of an integer, and accepts two parameters, one type of a character "cCharacter", and the second of the type integer "iTimes". The prototype would look like this: Code:
int foo(char, int); Code:
int (*fpFunction)(char, int); Code:
fpFunction = foo; Code:
fpFunction('a', 123); // One character, one integer.
This is probably the best tutorial on the internet about function pointers, if you want to read more about them: The Function Pointer Tutorials - Index
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum C/C++ resources - C/C++ frequently asked questions Python resources - Python frequently asked questions I'm always up for a chat, so feel free to contact me... |
|
|||
|
Forgot to include my errors.
Below are my errors while compiling... Errors ****** 1)In PFUNCPTR pfun declration, parse error before `;' ANSI C++ forbids declaration of `pFun' with no type 2)In the array decalration, 3 errors parse error before `;' parse error before `;' parse error before `;' 3)In (0 != SearchCommand...) ElementLocalCommandHandler.cpp:1659: implicit declaration of function `int SearchCommand (...)' 4)After that when it was assigned to command, ElementLocalCommandHandler.cpp:1662: parse error before `.' 5)In the search function, 6)`pFun' undeclared (first use this function) (Each undeclared identifier is reported only once for each function it appears in.) 7)In method `int FunctionEntry::SearchCommand (char *)': cannot convert `FunctionEntry' to `const char *' for argument `2' to `strcmp (const char *, const char *)' cannot convert `FunctionEntry *' to `int' in return I am just a learner and trying to do the same..May be the process I handled could be wrong.Could you please correct the code ..... Here are my answers & doubts ********************** 1)why cant I use #define and why it should be "const" 2)Attached the errors. 3)How should it be. Because my reqt is, int aa(int a); void bb(char a,char b); void cc(void); Can I implement these different signatures with function pointers??? 4)I am using the function pointer in my main function "Execute". thanks, Sowmi Thanks in advance, Sowmi |
|
|||
|
since my file contains other part of implementation I have given the snippet of my code alone..so, while compiling i got the above metnioned errors.
If possible can you correct the errors and make changes in my code itself.?? Thanks in advance, Sowmi |
| Sponsored Links |
|
|
|
|||||
|
The function pointer needs to match with the function you're going to point to. That's exactly how normal pointers works too. You can't make a pointer, int * and then point it to a character. They have to fit each other.
Read my last post, or visit the link I provided, to get all the answers you need. You're saying that the code snippets only is a part of an implementation, so I don't know if you've got the function pointers correct in some other part of the code - but in the snippets you provided, there were no valid function pointers.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum C/C++ resources - C/C++ frequently asked questions Python resources - Python frequently asked questions I'm always up for a chat, so feel free to contact me... |
|
|||
|
Thanks...
Just now I was looking into your example and also going thro' the website ... I think I need to learn the concepts well... Ok...Will try once I get the complete information about the fucntion pointers. Sowmi |
|
|||||
|
Quote:
Code:
#define pi 3.1415 int Compiler;
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
|
|||
|
Quote:
You are right in what you say about "#define"', but the above example would never happen. The "pi" in "Compiler" would not be replaced with 3.14515. That is a variable declaration, and therefore should not be changed. The variable Compiler does have the letters "pi" in it, but it's a completely different variable name. ![]() The preprocessor would only copy and paste itself when it encounters "pi", and no where else. |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Issue writing to file: pointer to a class which contains pointers to other classes | Sheemer | C and C++ | 0 | 08-21-2007 01:17 AM |
| C basics. | justin1993 | C and C++ | 4 | 07-24-2007 07:56 AM |
| What do pointers do? | Sionofdarkness | C and C++ | 15 | 06-15-2007 07:43 PM |
| Xav | ........ | 1357.94 |
| MeTh0Dz|Reb0rn | ........ | 1083.85 |
| WingedPanther | ........ | 919.18 |
| marwex89 | ........ | 906.86 |
| morefood2001 | ........ | 903.18 |
| John | ........ | 902.37 |
| Brandon W | ........ | 789.89 |
| chili5 | ........ | 312.39 |
| Steve.L | ........ | 264.99 |
| dcs | ........ | 240.34 |
Goal: 100,000 Posts
Complete: 83%