Here is what I am trying to do, I want the class A’s constructor to call B’s constructor for an object of B, like this main()->A->B
A.cpp:(included B.h)
A::A(){
B foo;
}
B.h:
class B{
B(){//some code};
};
but GCC won’t compile and says A::B foo has initializer but incomplete type. I am guessing the compiler saw no local class of B defined in A, so it complained and didn’t know the Class B was from another file. My question is how to construct an B’s object in A’s constructor like above.I am sure I am missing some fundamentals about C++, please bear with me. Thank you in advance.
You do not have any class of type
A::B. From your comment, it looks like you are trying to use a pointer toBby calling itA::B *. This is incorrect. A pointer toBis alwaysB *, regardless of where it appears. From what you said, it looks like you want something like this:a.hpp
a.cpp
b.hpp
b.cpp
main.cpp