function pointers?

Started by
4 comments, last by Shruubi 12 years, 4 months ago
i'm working on a console game, and am trying to implement a menu system, what i have so far is a function that will draw out the menu and take the input and return a value based on the menu choice, and i wanna use that return value as the parameter of another function which will perform actions based on what the choice was (play, load, credits, quit). my first attempt was to chuck the function as a normal function call in the parameter assuming that the return would be used as the parameter, but what actually happens is that the return of the functions used as the parameter will cause the calling function to return with the same value as well.

i'm assuming, therefore that i will need to use function pointers to pass the function in as a parameter and get the return value that way, however, for some reason my function parameters aren't working. any help would be much appreciated.


//prototypes
int menu();
int DoAction(int (*choice)());

//function implementation
int menu()
{
int x = 14;
int y = 0;
char input = '\0';

cout << "1: new game" << endl
<< "2: load game" << endl
<< "3: credits" << endl
<< "4: quit" << endl;

gotoxy(x, y);
cout << "<-";

while(true)
{
if(_kbhit())
{
input = _getch();

switch(input)
{
case UP_ARROW:
if(y > 0 && y < 4)
{
gotoxy(x, y);
cout << " ";
y--;
gotoxy(x, y);
cout << "<-";
break;
}
break;
case DOWN_ARROW:
if(y > -1 && y < 3)
{
gotoxy(x, y);
cout << " ";
y++;
gotoxy(x, y);
cout << "<-";
break;
}
break;
case 13: //enter was pressed
switch(y)
{
case 0:
return 0;
case 1:
return 1;
case 2:
return 2;
case 3:
return 3;
default:
break;
}
default:
break;
}
}
}

return 0;
}

int DoAction(int (*choice)())
{
int ch = (*choice)();
switch(ch)
{
case 0:
case 1:
case 2:
case 3:
exit(0);
default:
break;
}
return 0;
}

//function use (in main.cpp) - causes error: a value of type int (*)() cannot be used to initialize an entity of type int
int DoAction(&menu);


the error i am getting:

error: a value of type int (*)() cannot be used to initialize an entity of type int

thanks
Advertisement
int DoAction(&menu); isn't a function call. It's a declaration of a variable named DoAction of type int that you're trying to initialize with a function pointer. Remove the leading int from the statement.
I Have always found this page very helpful when dealing with function pointers,

-Josh

--www.physicaluncertainty.com
--linkedin
--irc.freenode.net#gdnet

Change:

int DoAction(&menu);

to

DoAction(&menu);

Also, it appears you are using conio.h and related functions. I would recommend not using them. conio.h is not a part of any standard. You mostly only see them being used in ancient C++ tutorials that predate the standardization.

int DoAction(&menu); isn't a function call. It's a declaration of a variable named DoAction of type int that you're trying to initialize with a function pointer. Remove the leading int from the statement.


oh wow, thats the kind of stupid mistake you make when your up at 2am after working customer service all day. thanks for spotting my blunder!

Change:

int DoAction(&menu);

to

DoAction(&menu);

Also, it appears you are using conio.h and related functions. I would recommend not using them. conio.h is not a part of any standard. You mostly only see them being used in ancient C++ tutorials that predate the standardization.


i use conio.h for kbhit() and getch() which, may be old and not standardized but seem to do the job, especially when i am basing movement around the arrow keys without having to delve into winapi.

This topic is closed to new replies.

Advertisement