枚举类型不能接受cin命令

Ins*_*Man 3 c++ enums

看看这段代码plz:

#include <iostream>
using namespace std;
int main()
{

    enum object {s,k,g};
    object o,t;

    cout << "Player One: "; cin >> o;
    cout << "Player Two: "; cin >> t;

    if (o==s && t==g) cout << "The Winner is Player One.\n";
    else if (o==k && t==s) cout << "The Winner is Player One.\n";
    else if (o==g && t==k) cout << "The Winner is Player One.\n";
    else if (o==g && t==s) cout << "The Winner is Player Two.\n";
    else if (o==s && t==k) cout << "The Winner is Player Two.\n";
    else if (o==k && t==g) cout << "The Winner is Player Two.\n";
    else cout << "No One is the Winner.\n";
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译时我会得到这个错误:'std :: cin >>''''''''''''''''''''''''''''''''''''''''''''''' 那么这段代码有什么问题?

小智 5

枚举没有运算符>>().你可以自己实现一个:

std::istream& operator>>( std::istream& is, object& i )
{
    int tmp ;
    if ( is >> tmp )
        i = static_cast<object>( tmp ) ;
    return is ;
}
Run Code Online (Sandbox Code Playgroud)

当然,如果你只是一个整数并自己投射会更容易.只是想告诉你如何写一个cin >>操作符.