Re: [問題] c++ class中帶有參數的成員初始化

看板Programming作者 (燒賣)時間17年前 (2007/12/16 10:08), 編輯推噓2(202)
留言4則, 3人參與, 最新討論串2/4 (看更多)
※ 引述《readonly (唯讀)》之銘言: : class A : { : public: : A( int c ){}; : ~A(){}; : }; : class B : { : public: : B(){}; : ~B(){}; : A a( 5 ); // 這樣不行嗎? : }; : 會出現下列的錯誤訊息: : error: expected identifier before numeric constant : error: expected `,' or `...' before numeric constant : error: ISO C++ forbids declaration of `parameter' with no type : 看來是把它當成一個function definition了, : 不知道正確的寫法是怎樣呢? 修正眼睛沾到狗屎的錯誤... class B { public: B() : a(5) {} ~B() {} A a; <-- 不過這東西還是放 private/protected 比較好 }; -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.135.82.153

12/16 19:36, , 1F
人家又沒說 B 要繼承 A -_-
12/16 19:36, 1F

12/16 19:42, , 2F
看起來應該是A a; 然後在B() : A(5) {}
12/16 19:42, 2F

12/16 20:53, , 3F
樓上也錯. 應該是 B() : a(5){}
12/16 20:53, 3F

12/16 21:16, , 4F
咦? 喔...眼殘看錯
12/16 21:16, 4F
※ 編輯: freesamael 來自: 220.135.82.153 (12/16 21:18)
文章代碼(AID): #17PFaowM (Programming)
討論串 (同標題文章)
文章代碼(AID): #17PFaowM (Programming)