C++标识符“_var”未定义
我正在尝试学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
_hero
仅在该switch
语句的范围内定义。您需要在与您将使用它们相同或更高的范围内创建对象。解决此问题的一种方法是在
switch
之前定义一个指向 Hero 的指针(初始化为null
),然后将其设置为switch
内的值代码>.例如:您还在类值上使用
->
(而不是指向一的指针)。除了范围问题之外,您可能打算编写_hero.GetName()
。在你的类中,->
是正确的,因为this
是指向你的对象的指针。_hero
is defined only within the scope of thatswitch
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 tonull
) and then set it to a value inside theswitch
. For instance: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 sincethis
is a pointer to your object._hero
的范围仅限于 switch 语句。The scope of
_hero
is limited to the switch statement.我认为这在 C# 中也不起作用...您想要的是一个将在 switch 语句中初始化的指针:
不过,您现在很可能需要一个虚拟析构函数和虚拟函数。仔细阅读它们,它们是一个强大的面向对象功能。但您可能通过 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:
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#.
你说你会一些 C# 和 php,但我不会。我只是想知道这在 C# 中会如何表现。
在某个范围内创建对象并在范围外使用它。例如:
{int a;} a = 0
;在 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.
_hero
对象仅限于该switch
块的范围。你想要的可能是这样的:The
_hero
object is restricted to the scope of thatswitch
block. What you want is probably this: