enum type can not accept cin command

前端 未结 3 2143
眼角桃花
眼角桃花 2020-12-03 09:43

Look t this code plz:

#include 
using namespace std;
int main()
{

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

    cout << \"Player One:          


        
3条回答
  •  猫巷女王i
    2020-12-03 09:54

    Do you expect to be able to type "s", "k", or "g" and have it parse those into your enum type? If so, you need to define your own stream operator, like this:

    std::istream& operator>>(std::istream& is, object& obj) {
        std::string text;
        if (is >> text) {
            if (is == "s") {
                obj = s;
            }
            // TODO: else-if blocks for other values
            // TODO: else block to set the stream state to failed
        }
        return is;
    }
    

提交回复
热议问题