FactoryMethod problem - header inclusion horror - expert
Header include horror - expert
Hello group.
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.
I have trivial StuffBase class and StuffDerived class that derives
from it. StuffBase has a pointer to Mother object instance. Mother has
a pointer to one StuffBase object instance. Mother has a method
CreateObject (const int) to create one StuffBase's instance. It uses
simple static StuffFactory that returns StuffBase*.
Problem seems easy, but during compilation MSVC++8 compiler returns
error: base class undefined for StuffDerived.h:
//--- File StuffDerived.h ---
#ifndef __stuffderived__h_
#define __stuffderived__h_
#include "stuffbase.h"
class StuffDerived : public StuffBase
{
public:
StuffDerived (const Mother* mother);
void DoSomething ();
virtual ~StuffDerived ();
} ;
#endif
//--- End file ---
When I remove #include "stufffactory.h" from mother.h and comment out
line that creates object out from factory everything compiles well.
Also when I call factory method in main (), "from outside" it works
well.
I hope experts can point out my error and explain why is it not
working properly. If not I hope you can provide me a solution.
Full code (10 files: StuffBase.h/.cpp, StuffDerived.h/.cpp,
Mother.h/.cpp, StuffFactory.h/.cpp, FactoryMethodProblem.cpp,
stdafx.h) is availible inline below, or here zipped :
http://www.haze.ehost.pl/pub/FactoryMethodProblem.zip .
Greetings,
Tomasz Kalkosi?ski
//--- File StuffBase.h ---
#ifndef __stuffbase__h_
#define __stuffbase__h_
#include "mother.h"
class Mother;
class StuffBase
{
protected:
int a;
public:
const Mother* mother;
StuffBase (const Mother* mother);
void DoSomething ();
virtual ~StuffBase ();
} ;
#endif
//--- End file ---
//--- File StuffBase.cpp ---
#include "stuffbase.h"
StuffBase::StuffBase (const Mother* mother) :
mother (mother),
a (1)
{}
void StuffBase::DoSomething ()
{}
StuffBase::~StuffBase ()
{}
//--- End file ---
//--- File StuffDerived.h ---
#ifndef __stuffderived__h_
#define __stuffderived__h_
#include "stuffbase.h"
class StuffDerived : public StuffBase
{
public:
StuffDerived (const Mother* mother);
void DoSomething ();
virtual ~StuffDerived ();
} ;
#endif
//--- End file ---
//--- File StuffDerived.cpp ---
#include "stuffderived.h"
StuffDerived::StuffDerived (const Mother* mother) :
StuffBase (mother)
{}
void StuffDerived::DoSomething ()
{}
StuffDerived::~StuffDerived ()
{}
//--- End file ---
//--- File Mother.h ---
#ifndef __mother__h_
#define __mother__h_
#include "stuffbase.h"
#include "stufffactory.h"
class StuffBase;
class Mother
{
public:
StuffBase* base;
Mother ();
void CreateObject (const int i);
} ;
#endif
//--- End file ---
//--- File Mother.cpp ---
#include "mother.h"
Mother::Mother ()
{}
void Mother::CreateObject (const int i)
{
this->base = StuffFactory::CreateObject (i, this);
}
//--- End file ---
//--- File StuffFactory.h ---
#ifndef __stufffactory__h_
#define __stufffactory__h_
#include "stuffbase.h"
#include "stuffderived.h"
class StuffFactory
{
public:
static StuffBase* CreateObject (const int i, const Mother*
mother);
} ;
#endif
//--- End file ---
//--- File StuffFactory.cpp ---
#include "stufffactory.h"
StuffBase* StuffFactory::CreateObject (const int i, const Mother*
mother)
{
switch (i)
{
case 1:
return new StuffDerived (mother);
default:
return new StuffBase (mother);
}
}
//--- End file ---
//--- File FactoryMethodProblem.cpp ---
// FactoryMethodProblem.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
#include "mother.h"
#include "stufffactory.h"
int _tmain(int argc, _TCHAR* argv[])
{
Mother* mother = new Mother ();
mother->CreateObject (0);
mother->base = StuffFactory::CreateObject (0, 0);
return 0;
}
/*
#include "stufffactory.h"
int _tmain(int argc, _TCHAR* argv[])
{
StuffBase* base = StuffFactory::CreateObject (1, 0);
return 0;
}
*/
//--- End file ---
//--- File stdafx.h ---
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#ifndef _WIN32_WINNT // Allow use of features specific to Windows XP
or later.
#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to
target other versions of Windows.
#endif
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
//--- End file ---
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]