C++: Calling class functions within a switch -


i've been trying study finals practicing classes , inheritance, i've come far inheritance , such i'm unsure how fix error occuring below.

#include<iostream> #include<iomanip> #include<cmath> #include<string.h> 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 << "name: " << name << endl;  cout << "main attribute: " << mainattr << endl;  cout << "xp: " << xp << endl;  cout << "hp: " << hp << endl;  cout << "mana: " << mana << endl;  cout << "armour: " << armour << endl;  cout << "attack range: " << range << endl;  cout << "attack damage: " << attkdmg << endl;  cout << "attack type: " << attktype  << endl << endl; }  void hero::getname() {      cout << "name: " << name << endl; }  void hero::getmainattr() {      cout << "main attribute: " << mainattr << endl; }  void hero::getattkdata() {      cout << "attack range: " << range << endl;      cout << "attack damage: " << attkdmg << endl;      cout << "attack type: " << attktype  << 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 << name << " has dropped following items: " << endl;      cout << drops << 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;          {       dispmenu();       cin >> choice;        switch (choice)       {       case 1:            system("cls");            cout << "please enter hero name: ";            cin >> heroname;            cout << "\nplease enter primary attribute\n";            cout << "1. strength\n" << "2. agility\n" << "3. intelligence\n";            cin >> 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 << "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 << "your hero has been created!\n\n";        player.dumpdata();        system("pause");         break;        }    case 2:        system("cls");        if (herocreated == 1)        {           cout << "your hero has been detailed below.\n\n";           **player.dumpdata(); //error occurs here !**           system("pause");        }        else        {            cout <<             "you have not created hero please exit prompt "            "and press 1 on menu create hero.";        }        break;    case 3:        system("cls");        cout << "still under development";        system("pause");        break;    case 4:        system("cls");        exit = 1;        break;    default:        cout << "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 <<      "1. create new hero\n"      "2. view current hero\n"      "3. fight stuff\n"           "4. exit\n\n"           "enter choice: "; }     

however upon compilation following errors:

220 `player' undeclared (first use function)  

unsure how fix i've started using oo approach. error has comment next above , in case 2 in main.

cheers guys.

there no player variable in scope of case need define player variable before whole switch clause if want of cases able access information. you'll need check if variable null or not or if object created yet before call method since won't guarantee it's made necessarily.

by doing this, bring variable player scope cases can see it:

hero player; switch () {     case 1:         player.dostuff();         break;     case 2:         player.domorestuff();         break; } 

if do

switch () {     case 1:         player.dostuff();         hero player;         break;     case 2:         player.domorestuff();         break; } 

then case 2 doesn't have hero player in scope (assume cases if/else clauses) in code it's more apparent because have braces around cases.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -