编译器错误“字符常量对于其类型来说太长”。(Compiler error “character constant too long for its type”. What's wrong?)

编程入门 行业动态 更新时间:2024-10-28 00:16:53
编译器错误“字符常量对于其类型来说太长”。(Compiler error “character constant too long for its type”. What's wrong?)

我有一些我正在努力的代码...

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

这是从我编写的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’

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

编辑:我现在得到一个新的错误消息...

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

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

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>

最满意答案

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

在C / C ++中,存在多字符文字这样的东西; 它的值是一个数字,它以某种方式将各个字符的字符代码以某种实现定义的方式组合在一起。 除非你真的有很好的理由,否则你不想使用它们。 他们只是需要了解它们的原因是了解警告和错误消息:

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

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

一旦解决了,你的新错误信息:

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

意味着其中一个|| 运营商。 也许它的一个操作数实际上不是一个布尔表达式。 “注释”告诉你有一个内置的|| 对于两个bool ,但它不能用于这种情况。

解决方案 :通过opt == "Yes"替换opt = 'Yes' 。

single = ,赋值意味着该表达式的结果不是一个bool而是一个字符串,并且没有operator|| 用于使用字符串构造一个布尔值。

样式注意:通常认为不使用using namespace std声明的样式更好。 相反,使用std::前缀显式引用标准库的东西( cout , endl , string , getline ),就像在std::string 。

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

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:

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.

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.

Solution: Replace opt = 'Yes' by 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.

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

更多推荐

本文发布于:2023-08-01 23:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1365177.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常量   编译器   于其   太长   字符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!