我有一些我正在尝试处理的代码...
#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.";
}
}
}
这是改编 self 编写的 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'
),否则它们就是字 rune 字。
在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 表达式。 “注释”告诉您有一个用于两个 bool
的内置 ||
,但在这种情况下不能使用它。
解决方案:将 opt = 'Yes'
替换为 opt == "Yes"
。
单个 =
赋值意味着该表达式的结果不是 bool 而是字符串,并且没有用于 or 运算的 operator||
带字符串的 bool 值。
风格注意:通常认为不使用 using namespace std
声明是更好的风格。相反,使用 显式引用标准库内容(
前缀,如 cout
、endl
、string
、getline
) std::std::string
中。
关于c++ - 编译器错误 "character constant too long for its type"。怎么了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9130112/