C++标识符“_var”未定义

发布于 2024-11-08 02:55:35 字数 1288 浏览 1 评论 0原文

我正在尝试学习 C++(目前只知道 PHP 和一些 C#)并遇到了我的第一个问题。

我试图在开关内调用一个类,然后在开关后使用该定义的变量。但是,我收到标题中描述的错误。

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

有问题的错误发生在以下行:

cout << _hero->getName();

其中说 _hero 未定义。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情域 2024-11-15 02:55:35

_hero 仅在该 switch 语句的范围内定义。您需要在与您将使用它们相同或更高的范围内创建对象。

解决此问题的一种方法是在 switch 之前定义一个指向 Hero 的指针(初始化为 null),然后将其设置为 switch 内的值代码>.例如:

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

if (_hero) {
    cout << _hero->GetName();
}

您还在类值上使用 -> (而不是指向一的指针)。除了范围问题之外,您可能打算编写 _hero.GetName()。在你的类中, -> 是正确的,因为 this 是指向你的对象的指针。

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

童话 2024-11-15 02:55:35
switch (hClass) {
    case 1:
        Wizard _hero;
        break;
} // <-- _hero is deallocated at this point

cout << _hero->GetName();

_hero 的范围仅限于 switch 语句。

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.

千纸鹤带着心事 2024-11-15 02:55:35

我认为这在 C# 中也不起作用...您想要的是一个将在 switch 语句中初始化的指针:

Hero* _hero = 0;

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

// use _hero ...

// at the end, delete it
delete _hero;

不过,您现在很可能需要一个虚拟析构函数和虚拟函数。仔细阅读它们,它们是一个强大的面向对象功能。但您可能通过 C# 了解了它们。

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

世界和平 2024-11-15 02:55:35

你说你会一些 C# 和 php,但我不会。我只是想知道这在 C# 中会如何表现。

在某个范围内创建对象并在范围外使用它。例如:{int a;} a = 0;

在 C++ 中这是一个问题。

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

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
巨坚强 2024-11-15 02:55:35

_hero 对象仅限于该 switch 块的范围。你想要的可能是这样的:

Hero* _hero;

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

cout << _hero->GetName();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文