2012-02-03 232 views
6

我有一些代碼,我試圖去努力......編譯器錯誤「字符常量對於其類型太長」。怎麼了?

#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> 
+1

爲了澄清它,C++不是一種腳本語言,所以這實際上不是一個腳本。 – 2012-02-03 14:25:42

+0

如果我們知道*實際上第19行是哪裏等,那麼這些錯誤將會更有用。它似乎不等於代碼中的行號,這使得我在發佈之前已經修改了代碼。 – 2012-02-03 14:28:17

+0

@NiklasR你能解釋一下嗎?另外,我沒有說這是。我說這是一個程序。那也錯了嗎? – CoffeeRain 2012-02-03 14:35:20

回答

18

正如其他人所指出的那樣,你需要用雙引號("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 s的內置||,但它不能用於這種情況。

解決方案:用opt == "Yes"替代opt = 'Yes'

單個=賦值意味着該表達式的結果不是一個布爾值而是一個字符串,並且沒有用於使用字符串進行布爾操作的operator||

款式注意:通常認爲不使用using namespace std聲明更好。相反,使用std::前綴明確指代標準庫內容(cout,endl,string,getline),如std::string中所述。

+0

謝謝!剛剛在C++聊天中提出了相同的答案 – CoffeeRain 2012-02-03 15:01:42

+0

感謝這樣一個很好的答案! – 0x6900 2014-10-24 07:30:16

1

您使用單引號括住的字符串。您需要更改

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 
+0

謝謝!我真的是一個Python人,所以我總是把字符串放在單引號中。 – CoffeeRain 2012-02-03 14:28:51

+1

我希望你有嘗試自學C++的真正理由。 – 2012-02-03 14:37:17

+0

請閱讀我的編輯。 – CoffeeRain 2012-02-03 14:39:32

相關問題