Variable declaration in if-else block in C ++

I am trying to declare a variable in an if-else block as follows:

int main(int argc, char *argv[]) { if (argv[3] == string("simple")) { Player & player = *get_Simple(); } else if (argv[3] == string("counting")) { Player & player = *get_Counting(); } else if (argv[3] == string("competitor")) { Player & player = *get_Competitor(); } // More code } 

But when trying to compile, I get the following errors:

driver.cpp: 38: error: unused variable 'Player
driver.cpp: 40: error: unused variable player

driver.cpp: 42: error: unused variable 'Player
driver.cpp: 45: error: Player was not declared in this volume

Any ideas?

+5
c ++ variables if-statement
source share
6 answers

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; } 
+20
source share

Others suggested pointers. However, a conditional statement can also be used.

 Player & player = argv[3] == string("simple") ? get_Simple() : argv[3] == string("counting") ? get_Counting() : get_Competitor(); 
+16
source share

If you put a static variable inside the scope marked with { } , then this variable will no longer be available when the scope expires.

Try this instead:

 int main(int argc, char *argv[]) { // TODO: validate argc and argv here if (argc < 3) { printf("error: not enough arguments\n"); exit(1); } Player* player_ptr = NULL; if (argv[3] == string("simple")) { player_ptr = get_Simple(); } else if (argv[3] == string("counting")) { player_ptr = get_Counting(); } else if (argv[3] == string("competitor")) { player_ptr = get_Competitor(); } if (!player_ptr) { printf("error: invalid argument %s\n", argv[3]); exit(1); } Player& player = *player_ptr; // More code } 
+3
source share

You specified three separate player variables in three different areas, and the error message says exactly what that means.

You need to declare a single player variable outside the if -statement and assign the result. This is difficult because the player is a link - you must initialize it once.

You can put an if -statement in a function (say GetPlayer() ) that returns a pointer to an object and then initializes the player with * GetPlayer ().

+2
source share

IN

 if (argv[3] == string("simple")) { Player & player = *get_Simple(); } 

A variable exists only between {} 's. When you go to } , the variable has not been used and will be discarded, but never used.

0
source share
 #include <map> int main(int argc, char **argv) { typedef std::map<std::string, Player*(*)()> lookup; lookup mapping; mapping["simple"] = get_Simple; mapping["counting"] = get_Counting; mapping["competitor"] = get_Competitor; lookup::const_iterator it = mapping.find(argv[3]); if (it == mapping.end()) { std::cout << "illegal argument\n"; } else { Player& player = *it->second(); // more code } } 
0
source share

All Articles