C++ 编译器错误";字符常量对于其类型“太长”;。什么';怎么了?

C++ 编译器错误";字符常量对于其类型“太长”;。什么';怎么了?,c++,compiler-errors,operators,C++,Compiler Errors,Operators,我有一些代码,我正在努力工作 #include <iostream> #include <string> int main() { std::cout << "Hello. Welcome to Delicious Drive Up. What would you like to order?\n"; std::cout << "\nOur menu is-"; std::cout << "..."; std::cou

我有一些代码,我正在努力工作

#include <iostream>
#include <string>

int main()
{
  std::cout << "Hello. Welcome to Delicious Drive Up. What would you like to order?\n";
  std::cout << "\nOur menu is-";
  std::cout << "...";
  std::cout << "\nOrder here > ";
  std::string choice;
  std::getline(cin, choice);
  if (choice == 'hamburger' || choice == 'Hamburger')
  {
      std::cout << "We don't have any ham. Is a Chickenburger all right? y/n. > ";
      std::string opt;
      std::getline(cin, opt);
      if (opt == 'y' || opt == 'Y' || opt == 'yes' || opt = 'Yes')
      {
          std::cout << "Here's your chickenburger.";
      }
  }
}
你能解释一下这些是什么意思以及如何修复它们吗

编辑:我现在收到一条新的错误消息

.test.cpp: In function ‘int main()’:
.test.cpp:23: error: no match for ‘operator||’ in ‘((std::operator== [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(& opt))), ((const char*)"y")) || std::operator== [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(& opt))), ((const char*)"Y"))) || std::operator== [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(& opt))), ((const char*)"yes"))) || opt’
.test.cpp:23: note: candidates are: operator||(bool, bool) <built-in>
.test.cpp:在函数“int main()”中:
.test.cpp:23:错误:中的“operator | |”不匹配((std::operator=[with | CharT=char,| Traits=std::char | Traits,| Alloc=std::allocator]((const std::basic|string&)(const std::basic|string*)(&opt)),((const char*))))| std::operator=[with | CharT=CharT=CharT,| std::allocator=[with | CharT=CharT=CharT,|(((const std::basic_string&)((const std::basic_string*)(&opt)),((const char*)“Y”))| std::operator=[带(CharT=char,_Traits=std::char_Traits,_Alloc=std::分配器]((const std::basic_string&)((const std::basic_string*)(&opt)),((const std::basic| basic(basic_string*)(&opt)),(const char*),(const char
注:候选者是:操作员| |(bool,bool)

您正在使用单引号将字符串括起来。您需要更改

if (choice == 'hamburger' || choice == 'Hamburger')

当然,这同样适用于
'Yes'
'Yes'

至于第二个问题,您试图将一个字符与一个字符串进行比较。您需要将您的<代码> y’/COD>作为字符串:

if (opt == "y" || opt == "Y" || opt == "yes" || opt == "Yes")
       //  ^^^ Note the double quotes also on single characters

正如其他人所指出的,您需要对字符串使用双引号(
“y”
,而不是
“y”
),否则它们就是字符文本

在C/C++中,有一种多字符文字;它的值是由以某种实现定义的方式将各个字符的字符代码组合在一起而成的数字。除非你有一个非常好的理由,否则你永远都不想使用它们。你需要了解它们的唯一原因是理解它们警告和错误消息:

test.cpp:19: error: no match for ‘operator==’ in ‘choice == 1919378802’
…意味着无法将字符串与数字1919378802进行比较,这是编译器解释为“汉堡”的意思

修复后,新的错误消息:

.test.cpp:23: error: no match for ‘operator||’ in ...
.test.cpp:23: note: candidates are: operator||(bool, bool) <built-in>
.test.cpp:23:错误:中的“operator | |”不匹配。。。
注:候选者是:操作员| |(bool,bool)
表示其中一个
|
运算符出错。可能它的一个操作数实际上不是布尔表达式。“注意”告诉您,两个
bool
都有一个内置的
|
,但在这种情况下不能使用

解决方案:用
opt==“Yes”
替换
opt==“Yes”

单个
=
赋值表示该表达式的结果不是布尔值而是字符串,并且没有
运算符| |
用于布尔值或将布尔值与字符串匹配


样式说明:通常认为更好的样式是不使用
使用名称空间std
声明。相反,请明确引用标准库内容(
cout
endl
string
getline
)使用
std::
前缀,就像在
std::string

中一样,您使用单引号将字符串括起来

if (choice == 'hamburger' || choice == 'Hamburger')
将其更改为双引号

if (choice == "hamburger" || choice == "Hamburger")
单引号用于定义单字符,双引号用于定义字符串文字

if (choice == 'hamburger' || choice == 'Hamburger')
if (choice == "hamburger" || choice == "Hamburger")