【问题标题】:cin >> operand cannot work with enumcin >> 操作数不能与枚举一起使用
【发布时间】:2014-04-18 05:08:19
【问题描述】:

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

【问题讨论】:

    标签: c++ enums cin operands


    【解决方案1】:

    上面写着“错误 2 错误 C2088: '>>' : 对类非法”和“错误 1 ​​错误 C2371: '选择' : 重新定义;不同的基本类型”我能得到一些关于如何解决这个问题的建议吗?

    好的,让我们看看你写了什么:

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

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

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

    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.
    

    【讨论】:

    • 关于原始问题,这绝对是最好的答案。
    • @Thomas: 题为“cin >> operand cannot work with enum”的问题不是吗?
    • @TonyD 你的回答在技术上是最优雅的,但如果这是一个初学者,可能有点多。我喜欢你在这方面的回答,但对于一般的学习用户来说,这将是我的答案。
    • 是的,我同意托马斯的观点。托尼有一个很好的答案,但对于一个试图快速修复编译错误的新手来说可能太多了。但我认为托尼的答案仍然有用,因为它揭示了枚举的通用运算符>>重载机制,尽管我认为,对于这个通用问题可能已经有了一些答案。
    【解决方案2】:

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

    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";
    }
    

    【讨论】:

    • 托尼,注意优先级。 "
    • @LaszloPapp 是的 - 我写它的时候也有同样的怀疑,但我懒得去查它,而且我通常更喜欢不那么杂乱的代码,即使这意味着要对一两个编译错误做出反应。不一定对可读性说太多,但在这种用法中,它只能尝试做一件事。人们应该学习三元运算符——越早越好。干杯。
    • Fwiw,我没有尝试编译,只是从偶然发现的经验中记住的。 :P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    相关资源
    最近更新 更多