作者freesamael (燒賣)
站內Programming
標題Re: [問題] c++ class中帶有參數的成員初始化
時間Sun Dec 16 18:08:46 2007
※ 引述《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
推 avhacker:人家又沒說 B 要繼承 A -_- 140.123.19.235 12/16 19:36
→ WPC001:看起來應該是A a; 然後在B() : A(5) {} 123.195.25.91 12/16 19:42
推 avhacker:樓上也錯. 應該是 B() : a(5){} 140.123.19.235 12/16 20:53
→ freesamael:咦? 喔...眼殘看錯 220.135.82.153 12/16 21:16
※ 編輯: freesamael 來自: 220.135.82.153 (12/16 21:18)