看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) g++ -std=c++11 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) N/A 問題(Question): 因為新學c++11, 我根本連compile error 要怎麼fix 都不知道 請各位大神指點看看 huffman.cc:22:23: error: object of type 'std::__1::unique_ptr<huffman_node, std::__1::default_delete<huffman_node> >' cannot be assigned because its copy assignment operator is implicitly deleted left_ = std::move(other.left_); ^ 程式碼(Code):(請善用置底文網頁, 記得排版) http://codepad.org/KP4m4CRE 感恩 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 50.185.236.143 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1455852253.A.435.html ※ 編輯: shaopin (50.185.236.143), 02/19/2016 11:33:18
Caesar08: http://ideone.com/j6dPld 02/19 11:43
Caesar08: 對一個const object做move,會return const T && 02/19 11:48
Caesar08: 一般來說,copy的parameter用const T &,move的則是T && 02/19 11:49
Caesar08: 當你return const T &&,他會被傳給copy而不是move 02/19 11:49
Caesar08: 因此你就會看到cannot be assigned because its copy 02/19 11:49
Caesar08: assignment operator is implicitly deleted 02/19 11:50
Caesar08: 但你卻覺得奇怪,明明你已經move了,怎麼還會用copy呢 02/19 11:50
Caesar08: 不過17行那邊應該使用move constructor,我只是示範給你 02/19 11:52
Caesar08: 看 02/19 11:52
shaopin: 感恩, 那這一行 02/19 13:40
shaopin: std::unique_ptr<huffman_node> p = std::move(nodes[i]) 02/19 13:40
shaopin: compile 也出現問題, 請問也是要用move ctor嗎 02/19 13:41
Caesar08: print_node有很大的問題,只是印出來,為甚麼要move? 02/19 14:17
BlazarArc: http://ideone.com/RLfbKX 這樣就好 print為何要move? 02/19 15:30
firose: copy應該指向新物件吧? 像raw ptr那樣, 不是在裡面move 02/19 16:02
firose: 否則好像應該用 shared_ptr 02/19 16:05