Search code examples
c++lvalue

C++ Error C2102: '&' requires l-value


I'm creating a console application in c++ with a lot of menus and sub-menus. The way I display my menu is through a do-while loop. I created a function to display the menu loop with three parameters - first being an integer that relates to how many options are in the menu, the second being a function displaying the menu and the third being another function which performs the selection inputed by the user.

class Menu {
public:

void displayInitialMenu(){
        system("cls");
    string menu = "\n\t\tXXXXXXXXX"\
      "\n\n Please select from the following:"\
      "\n\n 1. XXXXXXX"\
      "\n\n 2. XXXXXXX"\
      "\n\n 3. Exit\n";
    cout << menu << endl;
}



static bool checkOption (int option, int lower, int upper){
if ((option < lower) || (option > upper)){
    return false;   
} else {
    return true;
}
}

static int readOption(int lower, int upper){

    int option = 0;
    bool validMenuOption = false;

    do{
        std::cin >> option;
        validMenuOption = checkOption(option, lower, upper);

        if (!validMenuOption){
            std::cout << "\nError: Input must be between " << lower;
            std::cout << " and " << upper << "\n" << std::endl;     
        }
    } while (!validMenuOption);

    return option;
}
};

Menu menu;

void menuLoop(int numberOfOptions, void (*displayMenu)(), void (*switchStatement)()){
int menuOption = numberOfOptions;
do {
    (*displayMenu)();
    menuOption = menu.readOption(1, numberOfOptions);
    (*switchStatement)();
} while (menuOption != numberOfOptions);
}

static void performSelectionInitialMenu(int option){

switch (option){
case 1:
    break;

case 2:
    break;

case 3:
    break;

default:
    break;
}
}

int main()
{
/*
int menuOption = 3;
do {
    menu.displayInitialMenu();
    menuOption = menu.readOption(1, 3);
    performSelectionInitialMenu(menuOption);
} while (menuOption != 3);
*/

menuLoop(3, &menu.displayInitialMenu(), &performSelectionInitialMenu(3));

return 0;
}

The error I'm receiving is "error C2102: '&' requires l-value". I'm somewhat new to programming and this is the first time I'm passing through a function as a parameter. I'm making this function to eliminate code that I've commented out. Can anyone point to where I'm going wrong and a possible solution. If not, I'll just use duplicate code for each menu which I know is bad programming practice.


Solution

  • You are trying to take the addresses of values, returned by the functions displayInitialMenu and performSelectionInitialMenu, but both of these functions return nothing (void). Remove the & in front of both calls to fix this particular problem.