>"matches these operands"枚举-6ren"> >"matches these operands"枚举-所以,我完全是 c++ 语言的初学者,我正在做“通过游戏编程开始 C++”一书中的练习,特别是第 3 章的第一个练习。它要我创建一个“困难选择” Program"using std::cout 和 s-6ren">
gpt4 book ai didi

c++ - (C++) "No operator ">>"matches these operands"枚举

转载 作者:行者123 更新时间:2023-11-30 02:45:22 24 4
gpt4 key购买 nike

所以,我完全是 c++ 语言的初学者,我正在做“通过游戏编程开始 C++”一书中的练习,特别是第 3 章的第一个练习。它要我创建一个“困难选择” Program"using std::coutswitch 语句,但是我在尝试使用 std::cin 输入变量时发现了一个问题(myDifficulty) 的枚举类型 difficulties,我在 >>> 下得到上面的错误。这是我的代码:

#include <iostream>

int main()
{
enum difficulties {EASY = 1, NORMAL, HARD, EXTREME};
difficulties myDifficulty;

std::cout << "Easy\nNormal\nHard\nExtreme\n";
std::cin >> myDifficulty;

switch (myDifficulty)
{
case 1:
std::cout << "You have chosen Easy as your difficulty." << std::endl;
break;
case 2:
std::cout << "You have chosen Normal as your difficulty." << std::endl;
break;
case 3:
std::cout << "You have chosen Hard as your difficulty" << std::endl;
break;
case 4:
std::cout << "You have chosen Extreme as your difficulty. Good luck!" << std::endl;
break;
}
system("pause");
return 0;

}

如果有人可以帮忙,谢谢。此外,如果我的代码有任何问题(它的外观等),我们将不胜感激!

P.S 这是我的第一个问题。 :)

最佳答案

C++中enum类默认没有定义operator>>,需要自己创建operator:

std::istream& operator>> (std::istream& in, difficulties& diff) ;

关于c++ - (C++) "No operator ">>"matches these operands"枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24498708/

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