Re: FactoryMethod problem - header inclusion horror - expert
On 15 Okt., 22:08, Tomasz Kalkosi??ski <tomas...@poczta.onet.pl> wrote:
For a few days I have very strange problem in my C++ code. It's not
trivial, I use forward declarations, I use header inclusion guards but
it still won't work. I also showed this code to two fellow C++
programmers and we're still at dead end. Obviously there's something
wrong or I miss something.
Actually the problem is quite simple - Your header mother.h
and stuffbase.h do already have cyclic dependency - which is
easily to resolve:
!! Remove this include (you don't need it here, because the
following forward declaration of class Mother is sufficient
for this header):
The rest of stuffbase.h is fine so far.
//--- File StuffBase.cpp ---
OK, stuffbase.h is sufficient, no definition of Mother
required so far. Here is everything fine.
//--- File StuffDerived.h ---
OK, everything fine here. Mother is *declared* in
stuffbase.h, which is included above, declaration
is sufficient here. No problem here.
//--- File StuffDerived.cpp ---
Everything fine here, we need no more than this.
//--- File Mother.h ---
You don't need this dependency - just remove that include,
because *this* mother does not need to know her children ;-)
(Your following declaration of StuffBase is sufficient for
^ I see no reason to include this guy here - just remove it.
Rest of this header is OK.
//--- File Mother.cpp ---
Here you should add:
because you use contents of this header here.
*Iff* you need to delete the StuffBase* member
base in this file (which you don't show us, but what
is the most reasonable assumption), you also need
this guy, which adds the StuffBase definition:
Rest of cpp is fine.
//--- File StuffFactory.h ---
^^ Both includes are not needed, just add a single
declaration for StuffBase and Mother her:
The rest of the header is fine so far.
//--- File StuffFactory.cpp ---
Of-course you need to add all headers which bring
the *definition* of the types into scope, which are
created in this file:
#include "stuffderived.h" // Already includes StuffBase
Rest of cpp is fine.
//--- File FactoryMethodProblem.cpp ---
I ignore this stdafx stuff - it is not relevant here.
int _tmain(int argc, _TCHAR* argv)
I read this as:
int main(int argc, char* argv)
Mother* mother = new Mother ();
mother->base = StuffFactory::CreateObject (0, 0);
This should now work, because you provided the
necessary definitions for (a) Mother and (b) StuffFactory.
HTH & Greetings from Bremen,
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]