gpt4 book ai didi

c++ - 我需要使用枚举来表示难度级别。我一直收到有关使用 cin>> 的错误消息

转载 作者:行者123 更新时间:2023-11-28 00:24:40 25 4
gpt4 key购买 nike

我必须使用枚举来表示难度级别。这是代码。

// Menu Chooser
// Demonstrates the switch statement

#include <iostream>
#include <string>
using namespace std;

int main()
{
cout << "Difficulty Levels\n\n";
cout << "1 - Easy\n";
cout << "2 - Normal\n";
cout << "3 - Hard\n\n";

enum userChoice {Easy, Normal, Hard};
userChoice choice;
cout << "Choice: ";
cin >> choice;

switch (choice)
{
case 1:
cout << "You picked Easy.\n";
break;
case 2:
cout << "You picked Normal.\n";
break;
case 3:
cout << "You picked Hard.\n";
break;
default:
cout << "You made an illegal choice.\n";
}

return 0;
}

和错误信息:

error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)

最佳答案

>> 运算符没有默认实现来获取输入并将其映射到 enum。您需要将 int 作为输入,然后将其映射到 enum。或者,您可以为 >>> 运算符编写一个重载,将输入映射到您的枚举。

当然,查看您的代码,您似乎没有将定义的枚举用于任何事情。也许您可以删除它并改用 int?

关于c++ - 我需要使用枚举来表示难度级别。我一直收到有关使用 cin>> 的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25655088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com