2

这是我为练习而编写的代码。当我编译它时,它不允许编译 cin >> 选择。它显示“错误 2 错误 C2088: '>>' : 对类非法”和“错误 1 ​​错误 C2371: '选择': 重新定义;不同的基本类型” 我可以就如何解决这个问题获得一些建议吗?非常感激!

#include <iostream>

using namespace std;

int main()
{
    cout << "Difficulty levels\n\n";
    cout << "Easy - 0\n";
    cout << "Normal - 1\n";
    cout << "Hard - 2\n";

    enum options { Easy, Normal, Hard, Undecided };
    options choice = Undecided;
    cout << "Your choice: ";
    int choice;
    cin >> choice;

    switch (choice)
    {
    case 0:
        cout << "You picked Easy.\n";
        break;
    case 1:
        cout << "You picked Normal. \n";
        break;
    case 2:
        cout << "You picked Hard. \n";
        break;
    default:
        cout << "You made an illegal choice.\n";
    }

    return 0;
}
4

2 回答 2

6

它说“错误 2 错误 C2088:'>>':类非法”和“错误 1 ​​错误 C2371:'选择':重新定义;不同的基本类型”我可以就如何解决这个问题获得一些建议吗?

当然,让我们看看你写了什么:

...
options choice = Undecided;
// ^^^^^^^^^^^
cout << "Your choice: ";
int choice;
// ^^^^^^^^
cin >> choice;
..

这是个错误。首先,您应该只定义一次相同的变量。其次,枚举数没有 operator>> 重载,所以你不能使用前面的声明。

解决方案是删除前者,因此您将整体编写此代码(固定丑陋的缩进):

主文件

#include <iostream>

using namespace std;

int main()
{
    enum options { Easy, Normal, Hard, Undecided };
    cout << "Difficulty levels\n\n";
    cout << "Easy - " << Easy << "\n";
    cout << "Normal - " << Normal << "\n";
    cout << "Hard - " << Hard << "\n";
    cout << "Your choice: ";
    int choice;
    cin >> choice;

    switch (choice)
    {
    case Easy:
        cout << "You picked Easy.\n";
        break;
    case Normal:
        cout << "You picked Normal.\n";
        break;
    case Hard:
        cout << "You picked Hard.\n";
        break;
    default:
        cout << "You made an illegal choice.\n";
    }

    return 0;
}

输出

g++ main.cpp && ./a.out 
Difficulty levels

Easy - 0
Normal - 1
Hard - 2
Your choice: 0
You picked Easy.
于 2014-04-18T05:13:47.257 回答
4

您可以编写自己的运算符,这样您就不需要读取整数 - 这是一种方法:

bool consume(std::istream& is, const char* p)
{
    while (*p)
        if (is.get() != *p++)
        {
            is.setstate(std::ios::failbit);
            return false;
        }
    return true;
}

std::istream& operator>>(std::istream& is, options& o)
{
     switch (is.get())
     {
        case 'E': if (consume(is, "asy") { o = Easy; return is; } break;
        case 'H': if (consume(is, "ard") { o = Hard; return is; } break;
        case 'N': if (consume(is, "ormal") { o = Normal; return is; } break;
        case 'U': if (consume(is, "ndecided") { o = Undecided; return is; } break;
     }
     is.setstate(std::ios::failbit);
     return is;
}

同样,您可以编写一个输出运算符:

std::ostream& operator<<(std::ostream& os, options o)
{
    return os << (o == Easy ? "Easy" :
                  o == Hard ? "Hard" :
                  o == Normal ? "Normal" :
                  o == Undecided ? "Undecided" :
                                   "<invalid option>");
}

这些允许:

enum options { Easy, Normal, Hard, Undecided };

...streaming operators go here...

int main()
{
    cout << "Difficulty levels\n\n";
    cout << "Easy\n";
    cout << "Normal\n";
    cout << "Hard\n";

    options choice = Undecided;
    cout << "Your choice: ";
    if (cin >> choice)
        cout << "You picked " << choice << '\n';
    else
        cout << "Error while reading your choice.\n";
}
于 2014-04-18T05:32:05.543 回答