编译器错误"character constant too long for its type" 。怎么了?
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>
"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'
。
单个=
,赋值,意味着该表达式的结果不是布尔值,而是字符串,并且没有operator||
表示布尔值或用字符串表示布尔值。
样式注意:通常认为不使用using namespace std
声明是更好的样式。相反,使用std::
前缀显式引用标准库内容(cout
、endl
、string
、getline
),如std::string
中所示。
您使用单引号将字符串括起来。您需要更改
if (choice == 'hamburger' || choice == 'Hamburger')
至
if (choice == "hamburger" || choice == "Hamburger")
当然,'Yes'
和'yes'
也是如此。
至于第二个问题,您试图将单个字符与字符串进行比较。您也需要将'Y'
视为字符串:
if (opt == "y" || opt == "Y" || opt == "yes" || opt == "Yes")
// ^^^ Note the double quotes also on single characters
您正在使用单引号将字符串括起来。
if (choice == 'hamburger' || choice == 'Hamburger')
将其改为双引号。
if (choice == "hamburger" || choice == "Hamburger")
单引号用于定义单字符,双引号用于定义字符串文字。
相关文章:
- 使用 [] 运算符时"binding reference of type discards qualifiers"
- 在 QVector<std::unique_ptr 上使用 std::find<Type>>
- 在类型和包装器之间reinterpret_cast是否安全<Type>?
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- 控制到达非空函数clang(-Wreturn-type)的末尾
- std::unordered_map 类型对象声明期间出现"field has incomplete type"错误
- 将系数存储在头文件的数组中("does not name a type"错误)
- 尝试打开 ifstream 时出现"Incomplete type"错误
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- 在"结构提升::enable_if<提升::is_pod<T>,无效>"中没有名为"type"的类型
- "Missing type specifier - int assumed"无法通过向主函数添加"return 0"来解决
- OpenCV CV_16F type
- C++ "错误:在'类 std::result_of< ... >"中没有名为'type'的类型"
- 如何修复输出日志中的"EnableInput can only be specified on a Pawn for its Controller"错误
- 将内存分配返回值强制转换为 TYPE 数组
- C++ 一个lambda浅拷贝const Type&如果它被赋予一个命名捕获,如[copy=val](){}?
- reference_wrapper导致"incomplete type is not allowed"
- 继承类时"invalid use of incomplete type ‘class tree_node_t’"
- 由于"error C4430: missing type specifier - int assumed. Note: C++ does not support default-int",我现在无法编
- 编译器错误"character constant too long for its type" 。怎么了?