Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8086203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:22:36+00:00 2026-06-05T18:22:36+00:00

i’ve been trying to study for my finals by practicing classes and inheritance, this

  • 0

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.

#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 the 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;

    do
    {
      dispMenu();
      cin >> choice;

      switch (choice)
      {
      case 1:
           system("cls");
           cout << "Please enter your hero name: ";
           cin >> heroName;
           cout << "\nPlease enter your 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 a hero please exit this prompt "
           "and press 1 on the menu to create a 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 your choice: ";
}    

However upon compilation I get the following errors:

220 `player' undeclared (first use this function) 

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.

Cheers guys.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:22:37+00:00Added an answer on June 5, 2026 at 6:22 pm

    There is no player variable in scope of that case you need to define a player variable before the whole switch clause if you want all of the cases to be able to access that information. Also you’ll need to thus check if the variable is NULL or not or if object actually created yet before you call a method since it thus won’t guarantee it’s made necessarily.

    By doing this, you bring the variable player into scope so that all cases can see it:

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

    If you 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 are like if/else clauses) in your code it’s even more apparent because you have braces around your cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.