Your problem is that the player goes out of scope in each if / else if block.
You need to declare your variable above all if statements.
But you cannot use the link for this, because you must immediately initialize the link.
Instead, you probably want something like this:
int main(int argc, char *argv[]) { Player * pPlayer = NULL; if (argv[3] == string("simple")) { pPlayer = get_Simple(); } else if (argv[3] == string("counting")) { pPlayer = get_Counting(); } else if (argv[3] == string("competitor")) { pPlayer = get_Competitor(); } //Then if you really want to... Player &player = *pPlayer; }
Brian R. bondy
source share