#include <iostream>
int main()
{
const std::string exclam = "!";
const std::string message = "Hello" + ", world" + exclam;
std::cout << message;
return 0;
}
爲什麼此代碼不起作用?錯誤返回:無法在C++中添加字符串
error: invalid operands of types `const char[6]' and `const char[8]' to binary `operator+'
在此先感謝!
編輯:
感謝您的所有答案。這是我第一次在這個網站上,我很驚訝於在這麼短的時間內詳細解釋的數量。
關於實際問題。那麼這是怎麼來的呢:
const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";
是因爲「,世界」以及之後的「!」與變量hello(已定義)連接?
爲什麼不這樣做: const std :: string message =「Hello,world」+ exclam; – 2010-02-24 21:32:06
歡迎來到本網站!只是供參考,如果有你喜歡的答案,確保你接受它。你更有可能在未來獲得幫助。 – JasCav 2010-02-24 21:44:28