看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VC++ 2010 問題(Question): 在程式碼例1跟例2裡 只是簡單的將字串和在一起 為何在例2裡會出現編譯錯誤的訊息? 和例1的差別在何處? 程式碼(Code):(請善用置底文網頁, 記得排版) 例1 const std::string hello = "Hello"; const std::string message = hello + ", world" + "!"; 例2 const std::string exclam = "!"; const std::string message2 = "Hello" + ", world" + exclam; -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.254.16.42
tjjh89017:因為有const,所以不能更改 09/25 17:58
LPH66:不對...問題在於 "Hello" + ", world" 是 char * + char * 09/25 17:59
LPH66:hello + ", world" 是 std::string + char * 所以可以去找 09/25 18:00
LPH66:std::string 的 operator + (char *) 出來做事 09/25 18:00
LPH66:但 char * + char * 一來不合法 二來也不能重定義 09/25 18:00
gd03:感謝樓上的解釋 雖然還不是很懂 09/25 19:39
bestwishes:主要就是operator沒這個定義 自己OVERLOAD就可以了 09/25 20:35
uranusjr:究竟為什麼 C++ 沒有自己的 string literal 啊... 09/25 22:19
purpose:C-Style 字串不是用+號,直接寫 str = "Hello " "World"; 09/25 22:42
LPH66:......七樓有看到我說的不能重定義嗎 = =a 09/26 12:01