1

I am having issues with my switch function that made use of mapping to call functions from a class that I have created, and a switch function to pick them.

void MerkelMain::processUserOption()
{
    std::map<int, void(MerkelMain::*)()> menu;
    menu[1] = &MerkelMain::printHelp;
    menu[2] = &MerkelMain::printMarketStats;
    menu[3] = &MerkelMain::enterOffer;
    menu[4] = &MerkelMain::enterBid;
    menu[5] = &MerkelMain::printWallet;
    menu[6] = &MerkelMain::goToNextTimeFrame;

    switch (MerkelMain::getUserOption())
    {
    case 1:
        menu[1]();
        break;
    case 2:
        menu[2]();
        break;
    case 3:
        menu[3]();
        break;
    case 4:
        menu[4]();
        break;
    case 5:
        menu[5]();
        break;
    case 6:
        menu[6]();
        break;
    default:
        std::cout << "Invalid input. Please enter a value between 1 and 6." << std::endl;
    }
}

This is the compile error that I've received.

Any kind of help is greatly appreciated.

JeJo
  • 30,635
  • 6
  • 49
  • 88
oknotok
  • 33
  • 3

1 Answers1

1

All those map's values are member function pointers and needs special syntax to call.

For instance for your first map entry you need

(this->*menu[1])();
^^^^^^^^^^^^^^^^

Or use more generic function std::invoke (require C++17 or later)

#include <functional>  // std::invoke

std::invoke(menu[1], this);
JeJo
  • 30,635
  • 6
  • 49
  • 88