I can't get Visual C ++ Express (2010) to recognize the enum class

Hi guys (and girls), I am working on getting to know C ++ again after using Python too long. I wrote a small program with the MS Visual C ++ 2010 Express version, and I searched everywhere for the culprit why the compiler does not seem to like my use of the enum Choice class. The compiler complains that a namespace with this name does not exist. Now I have to say that all the previous C / C ++ code that I wrote was in academic mode, so I used the full IDE. Anyway, I am adding the code below and please forgive me if this is the wrong method for posting it. If this is the case, please refer to the correct method and I will use it from now on. Thank you in advance for any help or insight anyone can provide. The code is as follows.

#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 move Choice machine_choice; //holds machine 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; } 

Thanks again for taking the time to help me. I seem to remember how often I declared classes using C ++ and could not understand why he did not recognize it. Thanks again for taking the time. Court (Clemson University, South Carolina)

+4
source share
4 answers

VC ++ does not support the enum class in 2010. You need 2012 .

+6
source

enum class not supported in Visual C ++ 2010, I think it is in vC ++ 2012. See here .

You will need to update your compiler or use the usual "enum", which works a little differently, of course.

+5
source

So it looks like you want to use the "Select" namespace, but you did not define it first. To define a new namespace, define it as:

 namespace X { // namespace definition int a; int b; } using namespace X; //then you can use it 

But actually, I'm not sure if you need to define any namespace ... In your case, the problem that I identify is that you declare an "enum class Choice", not just an "enum Choice" . Please read, for example, this link about using enumerations in C ++: http://www.cplusplus.com/forum/beginner/44859/

I changed your code this way and it works fine:

 #include"stdafx.h" #include<iostream> #include<string> #include<ctime> using namespace std; enum Choice { rock, paper, scissors }; Choice player_choice; //holds user move Choice machine_choice; //holds machine 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
source

The enum class introduces as part of the C ++ 11 standard. It will not be in VC2010. To use this feature, you need to upgrade to VC2012. Else remove the "class" from the enum declaration.

 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. 
+1
source

Source: https://habr.com/ru/post/1483410/


All Articles