2013-05-29 43 views
4

嗨,大家好(和女孩), 我正在努力重新熟悉C++后,只使用Python太久。我已经用MS Visual C++ 2010 Express版编写了一个小程序,并且我已经遍地查找了为什么编译器似乎不喜欢使用enum类Choice的罪魁祸首。编译器抱怨这个名字的命名空间不存在。现在,我应该说我写的所有以前的C/C++代码都在学术环境中,因此我使用了完整的IDE。无论如何,我附上下面的代码,请原谅我,如果这是不正确的方法发布它。如果是这样,请参考我的正确方法,我将在今后使用它。提前感谢您提供任何帮助或洞见任何人可能能够借出。代码如下。我似乎无法获得Visual C++ Express(2010)识别枚举类

#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++来声明类,并且不知道为什么它不会识别它。再次感谢您抽出时间。法院(南卡罗来纳州克莱姆森大学)

+1

从枚举中删除类。并放弃选择命名空间行 - 那是试图完成什么? –

+0

[This](http://stackoverflow.com/questions/2603314/forward-strong-enum-in-vs2010)可能会有所帮助。看起来'enum class'在VS2010中不受支持。 – lcs

+0

感谢您花时间阅读它。我不确定我明白你的意思。班级正在努力完成什么? – court

回答

6

VC++在2010年不支持枚举类。您需要2012

+0

顺便说一句,我试图给你们投票,但它不会让我这样做。 - 法院 – court

+0

嗯,我想“部分”支持真的意味着“不”支持强类型的枚举......谢谢大家,再次,非常多的帮助,我很想成为社区的一部分。 -court – court

5

enum class在visual C++ 2010中不受支持,但我认为它在vC++ 2012中。 请参阅here

你将不得不升级你的编译器或者使用普通的“enum”,当然它的工作原理略有不同。

1

所以它看起来像你想使用名称空间“选择”,但你没有先定义它。 要定义一个新的命名空间,定义它像:

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; 
} 
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. 
+0

非常感谢。 – court