嗨伙计们(和女孩们), 我在使用Python太长时间之后,正在努力重新熟悉C ++。我已经用MS Visual C ++ 2010 Express版编写了一个小程序,我已经找到了一个罪魁祸首,为什么编译器似乎不喜欢我使用枚举类Choice。编译器抱怨不存在具有此名称的命名空间。现在,我应该说我之前编写的所有C / C ++代码都是在学术环境中,因此我使用的是完整的IDE。无论如何,我附上下面的代码,请原谅我,如果这是不正确的方法发布它。如果是,请参考我正确的方法,我将从此采用它。提前感谢您提供任何可能借出的帮助或见解。代码如下。
#include"stdafx.h"
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
enum class Choice { rock, paper, scissors };
using namespace Choice;**
Choice player_choice; //holds user's move
Choice machine_choice; //holds machine's move
string words[3] = {"rock","paper","scissors"};
Choice get_machine_choice();
void decide_winner();
string get_msg(Choice winner);
int rand0toN1(int n);
int main(int argc, char *argv[])
{
srand(time(NULL)); //set randomization
string input_str;
int c;
while (true) {
cout << "Enter Rock, Paper, Scissors, or Exit: ";
getline(cin, input_str);
if (input_str.size() < 1) {
cout << "Sorry, I don't understand that.\n";
continue;
}
c = input_str[0];
if (c == 'R' || c == 'r')
player_choice = rock;
else if (c == 'P' || c == 'p')
player_choice = paper;
else if (c == 'S' || c == 's')
player_choice = scissors;
else if (c == 'E' || c == 'e')
break;
else {
cout << "Sorry, I don't understand that.\n";
continue;
}
machine_choice = get_machine_choice();
int p = (int) player_choice;
int c = (int) machine_choice;
cout << "You Choose " << words [p];
cout << "," << endl;
cout << "I choose " << words [c];
cout << "," << endl;
decide_winner();
}
return EXIT_SUCCESS;
}
Choice get_machine_choice() {
int n = rand0toN1(3);
if (n == 0) return rock;
if (n == 1) return paper;
return scissors;
}
void decide_winner() {
if (player_choice == machine_choice) {
cout << "Reult is a tie.\n\n";
return;
}
int p = static_cast<int>(player_choice);
int c = static_cast<int>(machine_choice);
if (p - c == 1 || p - c == -2) {
cout << get_msg(player_choice);
cout << "Unfortunantly, you win...\n";
} else {
cout << get_msg(machine_choice);
cout << "I WIN, BEEEATCH!!!!\n";
}
cout << endl;
}
string get_msg(Choice winner) {
if (winner == rock)
return string("Rock smashes scissors, beeatch...");
else if (winner == paper)
return string("You know what paper does to rock, COVERAGE!!...");
else
return string("CHOP! Scissors cut paper!!....");
}
int rand0toN1(int n) {
return rand() % n;
}
再次感谢您抽出宝贵时间帮助我。我似乎记得经常使用C ++声明类,并且无法弄清楚它为什么不能识别它。再次感谢您抽出宝贵时间。法院(克莱姆森大学,SC)
答案 0 :(得分:6)
VC ++在2010年不支持enum类。您需要2012。
答案 1 :(得分:5)
enum class
,但我认为它在vc ++ 2012中。
见here。
您必须升级编译器或使用简单的“枚举”,当然这种方法稍有不同。
答案 2 :(得分:1)
所以看起来你想要使用命名空间“Choice”,但你没有先定义它。 要定义新的命名空间,请按以下方式对其进行定义:
namespace X { // namespace definition
int a;
int b;
}
using namespace X; //then you can use it
但事实上,我不确定你是否需要定义任何名称空间...... 在您的情况下,我确定的问题是您声明“枚举类选择”,而不是简单地“枚举选择”。请阅读以下关于c ++中枚举用法的链接:http://www.cplusplus.com/forum/beginner/44859/
我以这种方式修改了代码,运行正常:
#include"stdafx.h"
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
enum Choice {
rock,
paper,
scissors };
Choice player_choice; //holds user's move
Choice machine_choice; //holds machine's move
string words[3] = {"rock","paper","scissors"};
Choice get_machine_choice();
void decide_winner();
string get_msg(Choice winner);
int rand0toN1(int n);
int main(int argc, char *argv[])
{
srand(time(NULL)); //set randomization
string input_str;
int c;
while (true) {
cout << "Enter Rock, Paper, Scissors, or Exit: ";
getline(cin, input_str);
if (input_str.size() < 1) {
cout << "Sorry, I don't understand that.\n";
continue;
}
c = input_str[0];
if (c == 'R' || c == 'r')
player_choice = rock;
else if (c == 'P' || c == 'p')
player_choice = paper;
else if (c == 'S' || c == 's')
player_choice = scissors;
else if (c == 'E' || c == 'e')
break;
else {
cout << "Sorry, I don't understand that.\n";
continue;
}
machine_choice = get_machine_choice();
int p = (int) player_choice;
int c = (int) machine_choice;
cout << "You Choose " << words [p];
cout << "," << endl;
cout << "I choose " << words [c];
cout << "," << endl;
decide_winner();
}
return EXIT_SUCCESS;
}
Choice get_machine_choice() {
int n = rand0toN1(3);
if (n == 0) return rock;
if (n == 1) return paper;
return scissors;
}
void decide_winner() {
if (player_choice == machine_choice) {
cout << "Reult is a tie.\n\n";
return;
}
int p = static_cast<int>(player_choice);
int c = static_cast<int>(machine_choice);
if (p - c == 1 || p - c == -2) {
cout << get_msg(player_choice);
cout << "Unfortunantly, you win...\n";
} else {
cout << get_msg(machine_choice);
cout << "I WIN, BEEEATCH!!!!\n";
}
cout << endl;
}
string get_msg(Choice winner) {
if (winner == rock)
return string("Rock smashes scissors, beeatch...");
else if (winner == paper)
return string("You know what paper does to rock, COVERAGE!!...");
else
return string("CHOP! Scissors cut paper!!....");
}
int rand0toN1(int n) {
return rand() % n;
}
答案 3 :(得分:1)
枚举类是作为c ++ 11标准的一部分引入的。它不会存在于VC2010中。 您需要升级到VC2012才能使用此功能。否则从枚举声明中删除“class”。
enum class Choice { rock, paper, scissors }; //Error in vs2010. Drop class.
enum class Choice { rock, paper, scissors }; //Ok in vs2012.
enum Choice { rock, paper, scissors }; //Ok in vs2010.