[問題] constructor問題

看板C_and_CPP作者 (ken)時間6年前 (2018/05/27 23:13), 編輯推噓3(306)
留言9則, 3人參與, 6年前最新討論串1/1
開發平台(Platform): (Ex: Win10, Linux, ...) Linux 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) gcc 問題(Question): 我想要實作一struct 的 vector 但行數21~22與行數49行會互相排斥,把其中之一comment掉就不會有error 可是我兩個都需要。 程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔) https://pastebin.com/PUpj2iym 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.114.218.69 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1527434016.A.B29.html

05/27 23:21, 6年前 , 1F
需要一個data的default constructor
05/27 23:21, 1F

05/27 23:31, 6年前 , 2F
感謝你,是因為 string的size是未知嗎?所以它resize
05/27 23:31, 2F

05/27 23:31, 6年前 , 3F
不知道要給多少記憶體
05/27 23:31, 3F

05/27 23:40, 6年前 , 4F
好像不是這個原因,我好困惑
05/27 23:40, 4F

05/27 23:59, 6年前 , 5F
resize會用default建立物件 可是你寫了自己的ctor 所以
05/27 23:59, 5F

05/27 23:59, 6年前 , 6F
default(隱藏)的ctor就被蓋掉了 要自己補回來
05/27 23:59, 6F

05/28 00:02, 6年前 , 7F
你沒寫constructor 他會自動幫你產生default constructor
05/28 00:02, 7F

05/28 00:02, 6年前 , 8F
但是你寫了constructor 他就不會產生default constructor
05/28 00:02, 8F

05/28 02:02, 6年前 , 9F
我懂了,謝謝你們
05/28 02:02, 9F
文章代碼(AID): #1R2iiWif (C_and_CPP)