Jump to content
  • Advertisement
Sign in to follow this  
immanuelx2

compiler doesn't like me creating an object in a switch statement

This topic is 3707 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

int main()
{
     int cmd;
     int playerDmg = 14, playerHP = 100;
     int act = 0; //0 = adventure, 1 = encounter
     
     Player player1;
     vector<string> inventory;    

     inventory.push_back("sword");
     inventory.push_back("armor");
     inventory.push_back("shield");
     
     do
     {
         switch (act)
         {
             case 0: //adventuring
         
                showMenu();
                  
                cin >> cmd;
                cout << endl;
                  
                switch (cmd)
                {
                    case 0:
                        break;
                    case 1:
                        display(inventory);
                        break;
                    case 2:
                        Mob mob1;
                        cout << "\tYou find a " << mob1.getName() << "!" << endl << endl;
                        act = 1;
                        break;
                }   
                
                break;

            case 1: //encounter
        
            showEncounterMenu(mob1);
            
            cin >> cmd;
            cout << endl;
            
            switch (cmd)
            {
                case 1: 
                    if (!mob1.dmg(playerDmg)) act = 0; //if killing blow, end the encounter
                    else mob1.attack(playerHP); //else mob attacks back
                    break;
                case 2:
                    act = 0; //ends the encounter
                    break;
                default:
                    cout << "Invalid command" << endl;
            }
                   
            break;

        default:
            cout << "Invalid command" << endl;
        }
    }
    while (cmd != 0);
    
       
    return 0;
}

void showMenu()
{
     cout << "Enter a command:" << endl;
     cout << "================" << endl;
     cout << "1 - Show Inventory" << endl;
     cout << "2 - Explore" << endl;
     cout << "0 - Exit" << endl;
     cout << endl;
}

void showEncounterMenu(Mob& mob1)
{
     cout << "Enter a command:" << endl;
     cout << "================" << endl;
     cout << "1 - Attack " << mob1.getName() << endl;
     cout << "2 - Leave it alone" << endl;
     cout << endl;
}

void display(const vector<string>& vec)
{
     cout << "-------------" << endl;
     cout << "Inventory [" << vec.size() << "]" << endl;
     cout << "-------------" << endl;
     for (vector<string>::const_iterator iter = vec.begin(); iter != vec.end(); ++iter)
         cout << *iter << endl;
     cout << endl;
}
the error is on the line "showEncounterMenu(mob1);" stating "mob1 undeclared" the only way i can get this to work is to put the Mob mob1; as a global... however i only want to create the mob when the player inputs 2 (Explore). any ideas on how i should approach this??

Share this post


Link to post
Share on other sites
Advertisement
Use an anonymous scope:

// ...
case 2:
{ // start anon scope
Mob mob1;
cout << "\tYou find a " << mob1.getName() << "!" << endl << endl;
act = 1;
} // end anon scope
break;


Share this post


Link to post
Share on other sites
Quote:
Original post by immanuelx2
Quote:
Original post by rip-off
Use an anonymous scope:
*** Source Snippet Removed ***


didn't work. same error :(


That is because mob hasn't been declared in the right scope. The correction that rip-off and I suggested had to do with a different problem you had.

Share this post


Link to post
Share on other sites
Ah yes, I didn't read the entire code. You could do the following:

int main()
{
// variables
Mob mob;

do
{
switch(...)
{
case 2:
// provided your Mob class correctly implements the rule of three*
// we can "reset" its value like so
mob = Mob();
cout << "\tYou find a " << mob.getName() << "!" << endl << endl;
act = 1;
break;
// other case statements
}

// ...
// use "mob" later on


You could use pointers (or std::auto_ptr) and dynamic allocation, but that might complicate it a lot.

* Rule of Three

Share this post


Link to post
Share on other sites
Quote:
Original post by fpsgamer
Quote:
Original post by immanuelx2
Quote:
Original post by rip-off
Use an anonymous scope:
*** Source Snippet Removed ***


didn't work. same error :(


That is because mob hasn't been declared in the right scope. The correction that rip-off and I suggested had to do with a different problem you had.


hmm i see.. How should i go about this problem?

here is the concept of my simple text-based RPG:

1. Player is shown a list of a few options, using a do-while loop (1 - show inventory, 2 - explore, 0 - quit)
2. If a player selects 2 (explore) then a Mob is created.
3. act variable is then changed to 1: "encounter"
4. During the encounter, the player menu is changed to encounter menu and fights off the mob.
5. When the mob is killed, return player to main menu (step 1)

i am having trouble with getting step 2 to work.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Ah yes, I didn't read the entire code. You could do the following:
*** Source Snippet Removed ***
You could use pointers (or std::auto_ptr) and dynamic allocation, but that might complicate it a lot.

* Rule of Three


this worked. thank you very much :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!