Cof*_*ain 10 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::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.";
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是改编自我写的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’
Run Code Online (Sandbox Code Playgroud)
你能解释一下这些意思以及如何修复它们吗?
编辑:我现在收到一条新的错误消息...
.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>
Run Code Online (Sandbox Code Playgroud)
wol*_*ang 24
正如其他人指出的那样,你需要为你的字符串使用双引号("y"而不是'y'),否则它们就是字符文字.
在C/C++中,存在多字符文字这样的东西; 它的值是一个由某种实现定义的方式将各个字符的字符代码放在一起组成的数字.除非你有一个非常好的理由,否则你不想使用它们.他们只需要了解它们就是了解警告和错误消息:
test.cpp:19: error: no match for ‘operator==’ in ‘choice == 1919378802’
Run Code Online (Sandbox Code Playgroud)
...表示无法将字符串与数字1919378802进行比较,这是编译器解释'hamburger'的含义.
修复后,您的新错误消息:
.test.cpp:23: error: no match for ‘operator||’ in ...
.test.cpp:23: note: candidates are: operator||(bool, bool) <built-in>
Run Code Online (Sandbox Code Playgroud)
意味着其中一个||运营商出了问题.也许它的一个操作数实际上不是一个布尔表达式."注释"告诉您内置||了两个bools,但是在这种情况下无法使用它.
解决方案:替换opt = 'Yes'为opt == "Yes".
单个=赋值意味着该表达式的结果不是bool而是字符串,并且没有operator||用于或者带有字符串的布尔值.
风格注意:不使用using namespace std声明通常被认为是更好的风格.相反,明确提到标准库的东西(cout,endl,string,getline使用)std::的前缀,如std::string.
您使用单引号将字符串括起来。你需要改变
if (choice == 'hamburger' || choice == 'Hamburger')
Run Code Online (Sandbox Code Playgroud)
到
if (choice == "hamburger" || choice == "Hamburger")
Run Code Online (Sandbox Code Playgroud)
当然,同样的情况也适用于'Yes'和。'yes'
至于第二个问题,您试图将单个字符与字符串进行比较。您还需要将您视为'Y'字符串:
if (opt == "y" || opt == "Y" || opt == "yes" || opt == "Yes")
// ^^^ Note the double quotes also on single characters
Run Code Online (Sandbox Code Playgroud)