开发者

C++ Identifier "_var" is undefined

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.


_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.


switch (hClass) {
    case 1:
        Wizard _hero;
        break;
} // <-- _hero is deallocated at this point

cout << _hero->GetName();

The scope of _hero is limited to the switch statement.


I don't think that even works in C#... what you want is a pointer that's going to be initialized in the switch statement:

Hero* _hero = 0;

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

// use _hero ...

// at the end, delete it
delete _hero;

Though, you now most likely need a virtual destructor and virtual functions. Read up on them, they're a powerful OO feature. But you probably know about them from C#.


You said you know some C# and php, which I do not. I just want to know how would this have behaved in C#.

Creating an object inside some scope and using it outside the scope. Like: {int a;} a = 0;

In C++ its an issue.

    switch (hClass) {
    case 1:
        Wizard _hero;
        break;
    }
//At this no _hero is present. _hero is out of its scope


The _hero object is restricted to the scope of that switch block. What you want is probably this:

Hero* _hero;

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

cout << _hero->GetName();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜