4

あまりにも長い間 Python だけを使用してきた私は、C++ に再び慣れるために取り組んでいます。私は MS Visual C++ 2010 Express エディションで小さなプログラムを作成しましたが、コンパイラが enum クラス 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++ を使用してクラスを頻繁に宣言したことを覚えているようですが、なぜそれが認識されないのかわかりません。

4

4 に答える 4

6

VC++ は 2010 の enum クラスをサポートしていません。2012 が必要です

于 2013-05-29T16:41:42.983 に答える
5

enum classVisual C++ 2010 ではサポートされていませんでしたが、vc++ 2012 ではサポートされていると思います。ここを参照してください。

もちろん、コンパイラをアップグレードするか、わずかに異なる動作をする単純な「列挙型」を使用する必要があります。

于 2013-05-29T16:45:26.320 に答える
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;
} 
于 2013-05-29T16:54:58.137 に答える