我试图学习C++(目前只知道PHP和一些C#),并遇到我的第一个问题。C++标识符“_var”未定义
我想调用一个开关内的类,然后在开关后使用该定义的变量。但是,我收到标题中描述的错误。
#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是不确定的。
你需要的是一个工厂,返回你的“CharacterClass” /英雄基类的智能指针。当给定1(或用于指示“向导”的任何内容)时,工厂将返回Wizard类的实例。并给这个基类一个虚拟的析构函数。 – 2011-05-20 02:29:25
如果C++允许的话,那将是非常酷的......会节省很多头痛......有时候,C++的严格类型安全性是一种痛苦。 :P – Xeo 2011-05-20 02:37:00