【发布时间】:2017-05-10 19:00:31
【问题描述】:
我是 C++ 新手,最近开始为学校练习上课。 我真的看不出有什么问题,在为 Hero 类创建一个对象“玩家”后,我不能稍后在“主菜单”函数中使用该对象来调用方法,因为我得到“标识符未定义”错误!
有什么建议吗?
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Hero
{
private:
//member variables
string playername;
public:
//constructor
Hero(string name)
{
playername = name;
}
string getName()
{
return playername;
}
};
//start 1
void mainMenu()
{
cout << " - - - |" << player.getName() << "- - - \n";
}
void setPlayer()
{
string name;
cout << "Hello, what is your name? " << endl;
getline(cin, name);
Hero player(name);
mainMenu();
}
int main()
{
int selection;
cout << "Shadow of darkness\n ";
cout << "1.) Start ";
cout << "2.) Exit ";
cin >> selection;
if (selection == 1)
setPlayer();
else if (selection == 2)
exit (0);
else
main();
return 0;
}
【问题讨论】: