c++ - Function pointer loses its value -


i'm trying implement menu using map of chars , function pointers. program compiles fine, when try run pointers lose value along way.

i'm hoping guys might know it.

i'm using typedef:

typedef void (gui::*useraction)(); 

and following map:

map<char, useraction> menuoptions; 

here function registering pointers:

void gui::registeractions(){     menuoptions['c'] = &gui::addcd;     menuoptions['j'] = &gui::addjournal;     menuoptions['n'] = &gui::addnonfiction;     menuoptions['f'] = &gui::addfiction;     menuoptions['x'] = &gui::removeobject;     menuoptions['h'] = &gui::showhelpmenu;     menuoptions['s'] = &gui::search;     menuoptions['l'] = &gui::lenditem;     menuoptions['r'] = &gui::collectitem;     menuoptions['q'] = &gui::quit;     cout << menuoptions['c'] << endl; // writes 1 } 

i use template interpret user selection , return correct pointer:

template<typename option> option getoption(map<char, option> & options, char choise){     if(options.count(toupper(choise)) == 1){         cout << options[choise] << endl; // writes 0         return options[choise];     } else {         throw runtime_error("choise not match alternatives");     } } 

the choice made , functions called in following function:

void gui::showrequestmenu(){     try{         out << "choose 1 of c/j/n/f/x/h(help)/s/l/r/q" << endl;         useraction action = getoption(menuoptions, getchar());         cout << action ; // writes 0         (this->*action)(); // crashes program     } catch(exception & e){         out << e.what() << endl;     } } 

i've tried debugging program gdb , says

program recieved signal sigsev, segmentation fault 0x00000000 

the problem call toupper when checking if choice valid , don't later. simplest fix seems be:

change:

useraction action = getoption(menuoptions, getchar()); 

to:

useraction action = getoption(menuoptions, toupper(getchar())); 

and:

if (options.count(toupper(choise)) == 1){ 

to:

if (options.count(choise) == 1){ 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -