私はクラスAとB Bを持っているが、私は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);
}
の他のデータメンバーとBを初期化する必要があります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を初期化するにはどうすればよいですか?
感謝
このよう
なぜ5つの答えがあり、 "イニシャライザーリスト" < - そのインスタンス以外のインスタンスはありません? –