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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9130112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:20:37  来源:igfitidea点击:

Compiler error "character constant too long for its type". What's wrong?

c++compiler-errorsoperators

提问by CoffeeRain

I have some code that I'm trying to work on...

我有一些我正在尝试处理的代码......

#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.";
      }
  }
}

This was adapted from a Bash script I wrote and is one of my first C++ programs. When I compile this, it comes up with these errors...

这是改编自我编写的 Bash 脚本,是我的第一个 C++ 程序之一。当我编译这个时,它出现了这些错误......

test.cpp:19:15: warning: character constant too long for its type
test.cpp:19:40: warning: character constant too long for its type
test.cpp:23:44: warning: multi-character character constant
test.cpp:23:59: warning: multi-character character constant
test.cpp: In function ‘int main()':
test.cpp:19: error: no match for ‘operator==' in ‘choice == 1919378802'
test.cpp:19: error: no match for ‘operator==' in ‘choice == 1919378802'
test.cpp:23: error: no match for ‘operator==' in ‘opt == 'y''
test.cpp:23: error: no match for ‘operator==' in ‘opt == 'Y''
test.cpp:23: error: no match for ‘operator==' in ‘opt == 7955827'

Could you explain what these mean and how to fix them?

你能解释一下这些是什么意思以及如何解决它们吗?

EDIT: I get a new error message now...

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

.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>

回答by wolfgang

As others have pointed out, you need to use double quotes ("y"instead of 'y') for your strings, otherwise they are character literals.

正如其他人指出的那样,您需要对字符串使用双引号("y"而不是'y'),否则它们是字符文字。

In C/C++, there is such a thing as a multi-character literal; its value is a number made up of somehow putting the character codes for the individual characters together in some implementation-defined way. You don't want to ever use them unless you have a really really good reason. They only reason you need to know about them is to understand the warnings and error messages:

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

test.cpp:19: error: no match for ‘operator==' in ‘choice == 1919378802'

... means that there is no way to compare a string with the number 1919378802, which is what your compiler interprets 'hamburger'to mean.

... 意味着无法将字符串与数字 1919378802 进行比较,这就是您的编译器解释'hamburger'的含义。

Once that is fixed, your new error message:

一旦修复,您的新错误消息:

.test.cpp:23: error: no match for ‘operator||' in ...
.test.cpp:23: note: candidates are: operator||(bool, bool) <built-in>

means that something went wrong with one of the ||operators. Maybe one of its operands wasn't actually a boolean expression. The "note" tells you that there is a built-in ||for two bools, but that it couldn't be used in this situation.

意味着其中一位||操作员出了问题。也许它的一个操作数实际上不是布尔表达式。“注释”告诉您有一个内置的||两个bools,但是在这种情况下不能使用它。

Solution: Replace opt = 'Yes'by opt == "Yes".

解决方案:替换opt = 'Yes'opt == "Yes".

The single =, assignment, means that the result of that expression is not a bool but a string, and there is no operator||for or-ing a boolean with a string.

单个=, 赋值意味着该表达式的结果不是 bool 而是字符串,并且没有operator||for or-ing 带有字符串的布尔值。

Style Note: It's usually considered better style to not use a using namespace stddeclaration. Instead, explicitly refer to standard library stuff (cout, endl, string, getline) using a std::prefix, as in std::string.

样式说明:通常认为不使用using namespace std声明是更好的样式。相反,使用前缀显式引用标准库内容(cout, endl, string, getlinestd::,如std::string.

回答by Pietro Lorefice

You're using single quotes to enclose a string. You need to change

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

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

to

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

The same thing applies to 'Yes'and 'yes', of course.

当然,同样的事情也适用于'Yes''yes'

As for the second problem, you're trying to compare a single character with a string. You need to consider your 'Y'as a string too:

至于第二个问题,您正在尝试将单个字符与字符串进行比较。您也需要将您的'Y'视为字符串:

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