I'm trying to build a (text-based) menu system. The idea is that there is a "Menu" class which holds instances of class "MenuOption" in an array. MenuOption is basically a string (the text to be displayed) and a function pointer. However, the function pointer is for a function in the "CustomerService" class that calls the Menu class.
So basically, CustomerService tells Menu to send the menu display; it displays the menu and gets the user input (i.e. which option they want to select), and then it asks Menu to return the function pointer that is held in the MenuOption corresponding to the user's choice.
The current version compiles, and runs as far as displaying the menu and accepting the user input. However, when I come to retrieve the function pointer, I get an error (don't exactly know which error; I'm using Boost networking so the error displayed is an end of file, but this simply means that the server socket has shut down, as it would).
I've included all the relevant code below. Sorry for the code dump but I thought it might be easier to include everything at once. The code comes from the three classes, CustomerService, Menu and MenuOption.
Creating the menu (CustomerService):
1 2 3 4 5 6 7 8 9
|
void CustomerService::createMenu()
{
Menu menu = Menu();
menu.addOption(MenuOption("Purchase", purchase));
menu.addOption(MenuOption("Chat", chat));
menu.addOption(MenuOption("Quit", quit));
mainMenu = menu;
}
|
Headers for the functions that are pointed to by each MenuOption. These are static functions; I pass the actual CustomerService object (which inherits from class Server) to the function, so that the object's methods can be called within. Reason for this is because I couldn't figure out how to make a function pointer for a non-static member (if indeed it is even possible):
1 2 3
|
void CustomerService::purchase(Server * server)
void CustomerService::quit(Server * server)
void CustomerService::chat(Server * server)
|
The function that calls the menu option in CustomerService. optionNumber is the user's selection, and has previously been validated as an existing option:
1 2 3 4
|
void CustomerService::callMenuFunction(int optionNumber)
{
mainMenu.getFunction(optionNumber)(this);
}
|
The function in Menu class that is being called by CustomerService. "options" is a vector of MenuOptions:
1 2 3 4
|
functionType Menu::getFunction(int index)
{
return options.at(index - 1).getFunction();
}
|
The getFunction() method of MenuOption:
1 2 3 4
|
functionType MenuOption::getFunction()
{
return function;
}
|
The typedef of "functionType":
typedef void(*functionType)(Server * server);
Finally, the MenuOption class constructor:
1 2 3
|
MenuOption::MenuOption(string optionText, void (*optionFunction)(Server * server)) : text(optionText), function(optionFunction)
{
}
|
Hopefully somebody can give me some advice. I'm guessing the problem lies in the first function shown (callMenuFunction in CustomerService), considering it compiles ok. Any help would be greatly appreciated.