title: C++: Calling class functions within a switch
body: <p>i've been trying to study for my finals by practicing classes and inheritance, this is what I've come up with so far for inheritance and such however I'm unsure how to fix the error occuring below.  </p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;iomanip&gt;
  #include&lt;cmath&gt;
  #include&lt;string.h&gt;
  using namespace std;
  
  
  //BASE CLASS DEFINITION
  
  class hero
  {     
   protected:
              string name;
              string mainAttr;
              int xp;
              double hp;
              double mana;
              double armour;
              int range;
              double attkDmg;
              bool attkType;
    public:
           void dumpData();
           void getName();
           void getMainAttr();
           void getAttkData();
           void setAttkData(string);
           void setBasics(string, string, double, double, double);
           void levelUp();
  };
  
  //CLASS FUNCTIONS
  
  void hero::dumpData()
  {
   cout &lt;&lt; "Name: " &lt;&lt; name &lt;&lt; endl;
   cout &lt;&lt; "Main Attribute: " &lt;&lt; mainAttr &lt;&lt; endl;
   cout &lt;&lt; "XP: " &lt;&lt; xp &lt;&lt; endl;
   cout &lt;&lt; "HP: " &lt;&lt; hp &lt;&lt; endl;
   cout &lt;&lt; "Mana: " &lt;&lt; mana &lt;&lt; endl;
   cout &lt;&lt; "Armour: " &lt;&lt; armour &lt;&lt; endl;
   cout &lt;&lt; "Attack Range: " &lt;&lt; range &lt;&lt; endl;
   cout &lt;&lt; "Attack Damage: " &lt;&lt; attkDmg &lt;&lt; endl;
   cout &lt;&lt; "Attack Type: " &lt;&lt; attkType  &lt;&lt; endl &lt;&lt; endl;
  }
  
  void hero::getName()
  {
       cout &lt;&lt; "Name: " &lt;&lt; name &lt;&lt; endl;
  }
  
  void hero::getMainAttr()
  {
       cout &lt;&lt; "Main Attribute: " &lt;&lt; mainAttr &lt;&lt; endl;
  }
  
  void hero::getAttkData()
  {
       cout &lt;&lt; "Attack Range: " &lt;&lt; range &lt;&lt; endl;
       cout &lt;&lt; "Attack Damage: " &lt;&lt; attkDmg &lt;&lt; endl;
       cout &lt;&lt; "Attack Type: " &lt;&lt; attkType  &lt;&lt; endl;
  }
  
  void hero::setAttkData(string attr)
  {
       int choice = 0;
  
       if (attr == "Strength")
   {
            choice = 1;
   }
   if (attr == "Agility")
   {
            choice = 2;
   }
   if (attr == "Intelligence")
   {
            choice = 3;
   }
  
   switch (choice)
   {
          case 1:
               range = 128;
               attkDmg = 80.0;
               attkType = 0;
               break;
  
          case 2:
               range = 350;
               attkDmg = 60.0;
               attkType = 0;
               break;
  
          case 3:
               range = 600;
               attkDmg = 35.0;
               attkType = 1;
               break;
  
          default:
                  break;
   }
  }
  
  void hero::setBasics(string heroName, string attribute, double health, double mp, double armourVal)
  {
       name = heroName;
       mainAttr = attribute;
       hp = health;
       mana = mp;
       armour = armourVal;
  }
  
  void hero::levelUp()
  {
       xp = 0;
       hp = hp + (hp * 0.1);
       mana = mana + (mana * 0.1);
       armour = armour + ((armour*0.1) + 1);
       attkDmg = attkDmg + (attkDmg * 0.05);
  }
  
  //INHERITED CLASS DEFINITION
  
  class neutHero : protected hero
  {
        protected:
              string drops;
              int xpGain;
        public:
           int giveXP(int);
           void dropItems();
  };
  
  //INHERITED CLASS FUNCTIONS
  
  int neutHero::giveXP(int exp)
  {
      xp += exp;
  }
  
  void neutHero::dropItems()
  {
       cout &lt;&lt; name &lt;&lt; " has dropped the following items: " &lt;&lt; endl;
       cout &lt;&lt; drops &lt;&lt; endl;
  }
  
  /*
    END OF OO!
  */
  
  //FUNCTION PROTOTYPES
      void dispMenu();
  
  
  int main()
  {
      int exit=0, choice=0, mainAttrChoice=0, heroCreated=0;
      double health, mp, armourVal;
      string heroName, attribute;
  
      do
      {
        dispMenu();
        cin &gt;&gt; choice;
  
        switch (choice)
        {
        case 1:
             system("cls");
             cout &lt;&lt; "Please enter your hero name: ";
             cin &gt;&gt; heroName;
             cout &lt;&lt; "\nPlease enter your primary attribute\n";
             cout &lt;&lt; "1. Strength\n" &lt;&lt; "2. Agility\n" &lt;&lt; "3. Intelligence\n";
             cin &gt;&gt; mainAttrChoice;
             switch (mainAttrChoice)
             {
                case 1:
                     attribute = "Strength";
                     health = 750;
                     mp = 150;
                     armourVal = 2;
                     break;
  
                case 2:
                     attribute = "Agility";
                     health = 550;
                     mp = 200;
                     armourVal = 6;
                     break;
  
                case 3:
                     attribute = "Intelligence";
                     health = 450;
                     mp = 450;
                     armourVal = 1;
                     break;
                default:
                     cout &lt;&lt; "Choice invalid, please try again.";
                     exit = 1;
                     break;
  
  
         hero player;
         player.setBasics(heroName, attribute, health, mp, armourVal);
         player.setAttkData(attribute);
         heroCreated=1;
         system("cls");
         cout &lt;&lt; "Your hero has been created!\n\n";
         player.dumpData();
         system("pause");
  
         break;
  
        } 
    case 2:
         system("cls");
         if (heroCreated == 1)
         {
            cout &lt;&lt; "Your hero has been detailed below.\n\n";
            **player.dumpData(); //ERROR OCCURS HERE !**
            system("pause");
         }
         else
         {
             cout &lt;&lt; 
             "You have not created a hero please exit this prompt "
             "and press 1 on the menu to create a hero.";
         }
         break;
  
    case 3:
         system("cls");
         cout &lt;&lt; "Still Under Development";
         system("pause");
         break;
  
    case 4:
         system("cls");
         exit = 1;
         break;
  
    default:
         cout &lt;&lt; "Your command has not been recognised, please try again.\n";
         system("pause");
         break;
    }
  }
  while (exit != 1);
  
  system("pause");
  return 0;
  
  }
  
  void dispMenu()
  {
       system("cls");
       cout &lt;&lt;
       "1. Create New Hero\n"
       "2. View Current Hero\n"
       "3. Fight Stuff\n"     
       "4. Exit\n\n"     
       "Enter your choice: ";
  }    
  </code></pre>
  
  <p>However upon compilation I get the following errors:</p>
  
  <pre><code>220 `player' undeclared (first use this function) 
  </code></pre>
  
  <p>Unsure exactly how to fix it as I've only recently started using OO approach. The error has a comment next to it above and is in case 2 in the main.</p>
  
  <p>Cheers guys.</p>