2013-05-29 69 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