枚举类型不能接受cin命令 [英] enum type can not accept cin command
本文介绍了枚举类型不能接受cin命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
查看此代码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;
}
在编译时我会得到这个错误: in'std :: cin >> o'
我使用代码块。所以这个代码有什么问题?
while compiling I will get this error:no match for 'operator>>' in 'std::cin >> o' I'm using code-blocks. so what is wrong with this code?
推荐答案
枚举没有运算符>>您可以自行实现:
There is no operator>>() for enum. You can implement one yourself:
std::istream& operator>>( std::istream& is, object& i )
{
int tmp ;
if ( is >> tmp )
i = static_cast<object>( tmp ) ;
return is ;
}
当然,如果你只是一个整数, 。只是想告诉你如何写一个cin >>运算符。
Of course, it would be easier if you just cin an integer and cast yourself. Just want to show you how to write an cin >> operator.
这篇关于枚举类型不能接受cin命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文