我有A類和B B是A 的一員,我需要與A的其他數據成員初始化乙如何用此類的其他成員數據初始化類成員數據?
class A;
class B
{
public:
B(A& a){cout << "B constr is run \n";}
};
class A
{
public:
A(){}
void initB(A& a){b(a); cout << "A call init B \n"; }
private:
// other members ...
B b;
};
int main()
{
A a;
a.initB(a);
}
我得到了編譯錯誤:
classIns.cpp: In constructor âA::A()â:
classIns.cpp:14: error: no matching function for call to âB::B()â
classIns.cpp:8: note: candidates are: B::B(A&)
classIns.cpp:6: note: B::B(const B&)
classIns.cpp: In member function âvoid A::initB(A&)â:
classIns.cpp:16: error: no match for call to â(B) (A&)â
爲什麼A(){}需要調用B :: B()?
如何用A的其他數據成員初始化B?
感謝
爲什麼有五個答案,沒有「初始化程序列表」的實例< - 除了一個? –