Ian Burris
Ian Burris

Reputation: 6515

Declaring a variable in an if-else block in C++

I'm 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, I'm getting the following errors when I try to compile:

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 scope

Any ideas?

Upvotes: 6

Views: 17883

Answers (6)

Parappa
Parappa

Reputation: 7676

If you put a static variable inside of a scope, delimited by { }, then that variable will no longer be available when the scope ends.

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
}

Upvotes: 3

Fred
Fred

Reputation: 162

#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
    }
}

Upvotes: -1

rlbond
rlbond

Reputation: 67749

Others have suggested pointers. However, the conditional operator may be used as well.

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

Upvotes: 17

Adrian McCarthy
Adrian McCarthy

Reputation: 47954

You've declared three separate player variables in three different scopes, and the error message is saying exactly what it means.

You need to declare a single player variable outside the if-statement and assign the result. This is tricky, since player is a reference--you must initialize it once.

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

Upvotes: 2

Brian R. Bondy
Brian R. Bondy

Reputation: 347216

Your problem is that player falls out of scope in each if / else if block.

You need to declare your variable above all of the if statements.

But you can't use a reference for that because you must initialize a reference right away.

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;

}

Upvotes: 22

S.Lott
S.Lott

Reputation: 391818

In

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

The variable only exists between the {}'s. When you get to the }, the variable has not been used and will be discarded, never having been used.

Upvotes: 0

Related Questions