我有一些代碼,我試圖去努力......編譯器錯誤「字符常量對於其類型太長」。怎麼了?
#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>
爲了澄清它,C++不是一種腳本語言,所以這實際上不是一個腳本。 – 2012-02-03 14:25:42
如果我們知道*實際上第19行是哪裏等,那麼這些錯誤將會更有用。它似乎不等於代碼中的行號,這使得我在發佈之前已經修改了代碼。 – 2012-02-03 14:28:17
@NiklasR你能解釋一下嗎?另外,我沒有說這是。我說這是一個程序。那也錯了嗎? – CoffeeRain 2012-02-03 14:35:20