Re: [問題] c++ class中帶有參數的成員初始化
※ 引述《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
12/16 19:36, 1F
→
12/16 19:42, , 2F
12/16 19:42, 2F
推
12/16 20:53, , 3F
12/16 20:53, 3F
→
12/16 21:16, , 4F
12/16 21:16, 4F
※ 編輯: freesamael 來自: 220.135.82.153 (12/16 21:18)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 4 篇):