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

  • SEARCH
  • Home
  • 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 5932403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:49:46+00:00 2026-05-22T14:49:46+00:00

I am attempting to learn C++ (currently only know PHP and some C#) and

  • 0

I am attempting to learn C++ (currently only know PHP and some C#) and have run into my first issue.

I am trying to call a class inside a switch, then use that defined variable after the switch. However, I get the error described in the title.

#include <iostream>
#include <string>

using namespace std;

class Hero {
protected:
    int hHealth,hStamina,hExp;
    string hName;
public:
    void Create(string);
    string GetName() {
        return this->hName;
    }
};

class Wizard:public Hero {
public:
    void SetStats(string hName) {
        this->hName = hName;

        this->hHealth = 40;
        this->hStamina = 80;
    }

};

int main() {
    string hName;
    int hClass;


    cout << "Welcome to Ryan's Dungeons & Dragons Adventure!\n\n";
    cout << "Enter your Heroes name\n";
    cout << "Name: ";
    cin >> hName;

    cout << hName << ", please select your class\n";
    cout << "(1) The Wizard\n";
    cout << "(2) The Warrior\n";
    cout << "(3) The Rogue\n";
    cout << "(4) The Priest\n";

    cout << "Class: ";
    cin >> hClass;

    switch (hClass) {
    case 1:
        Wizard _hero;
        break;
    }

    cout << _hero->GetName();


    system("PAUSE");
    return 0;
}

The error in question occurs on the line:

cout << _hero->getName();

where it says _hero is undefind.

  • 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-05-22T14:49:46+00:00Added an answer on May 22, 2026 at 2:49 pm

    _hero is defined only within the scope of that switch statement. You need to create objects in the same or higher up scope that you’ll be using them.

    One way you can get around this is define a pointer to Hero before the switch (initializing to null) and then set it to a value inside the switch. For instance:

    Wizard *_hero = NULL;
    switch (hClass) {
        case 1:
            _hero = new Wizard();
            break;
        }
    }
    
    if (_hero) {
        cout << _hero->GetName();
    }
    

    You’re also using the -> on a class value (as opposed to a pointer to one). Scope issues aside, you probably intended to write _hero.GetName(). Inside your class, -> is right however since this is a pointer to your object.

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

Sidebar

Related Questions

Currently, I'm trying to learn Javascript by writing my code in Gedit then attempting
I am attempting to learn oCaml and am having an issue as to why
I'm trying to learn python and I'm attempting a hangman game. But when I
I am attempting to learn about creating windows in c++, I have looked at
I'm attempting to learn some flex/bison, and I'm reading Flex & Bison by John
I'm attempting to learn Spring MVC but have been spoiled by Grails. While I
I am attempting to learn the PyMT library and am trying to implement a
I'm currently building my first database in MySQL with an interface written in PHP
I'm attempting to learn Haskell by writing a SIP client. The question I have
So I am attempting to learn how to code my first BST, and it

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.