cin>>操作数不能与枚举一起使用

时间:2014-04-18 05:08:20

标签: c++ enums cin operands

这是我为练习所做的代码。当我编译它时,它不允许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;
}

2 个答案:

答案 0 :(得分:6)

  

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

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

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

这是一个错误。首先,您应该只定义一次相同的变量。其次,普查员没有运营商&gt;&gt;重载,所以你不能使用前一个声明。

解决方案是删除前者,所以你将整体写出来(修复丑陋的缩进):

的main.cpp

#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.

答案 1 :(得分: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";
}