Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Namespace problems

Credits go to aj5555 and Bob for helping out!
This thread is locked; no one can reply to it. rss feed Print
Namespace problems
raycrasher
Member #6,683
December 2005

Take the following:

1 
2// File: engine.h
3#header guard
4#include "graphics.h" // for the camera below
5 
6// adding 'class Camera;' here reduced the errors somewhat, but i think its wrong, w/o the namespace and everything.
7 
8namespace Engine {
9 class Object {
10 ...
11 virtual void draw(Graphics::Camera &cam) {};
12 };
13}
14 
15// File: graphics.h
16#header guard
17#include "engine.h" // for Object list below
18 
19// adding 'class Object;' here reduced the errors somewhat, but i think its wrong, w/o the namespace and everything.
20 
21namespace Graphics {
22 class Camera {
23 Engine::Object *focus;
24 };
25}

The problem is, I get plenty of compile errors - "Undefined: Engine" in graphics.h and/or "Undefined: Graphics" in engine.h. I finally solved the problem by limiting the contents of my namespaces to functions and variables, but no class/struct defs. I'm just wondering what caused the errors.

aj5555
Member #9,033
September 2007

circular dependancies.

which means you have a bad design.

circular dependancies -> which came first chicken or the egg?

OnlineCop
Member #7,919
October 2006
avatar

aj5555 said:

circular dependancies -> which came first chicken or the egg?

The egg, of course! With the "evolution theory", the last "mutation" of the chicken's species was transferred into the now-layed egg. The egg, being a mutant of the "almost chicken" parent, can now officially be called "a chicken". Once it is hatched, it can be considered a full-fledged chicken, and when it mates with the "almost a chicken" species, natural selection allows it to keep its "chicken" classification and life is good. Colonel Sanders is soon spotted on the horizon.

Neil Black
Member #7,867
October 2006
avatar

Quote:

Colonel Sanders is soon spotted on the horizon.

And almost-chicken tells chicken, "Good luck, loser!" and runs off leaving chicken to its horrible (but delicious) fate. Ironically, almost-chicken was then eaten by a saber-toothed tiger, which sucks because, had almost-chicken survived, we would have had a food source that did not, in fact, taste like chicken.

Bob
Free Market Evangelist
September 2000
avatar

Simply forward-declare your classes.

// In engine.h
namespace Graphics {
    class Camera;
}

Hope this helps.

--
- Bob
[ -- All my signature links are 404 -- ]

raycrasher
Member #6,683
December 2005

Yes, you're right. It was a crappy design, so I overhauled the code, used singletons for Engine and Graphics and whatnot. It compiles okay now, and looks more readable, too. Thanks.

Now, if I can only make OpenLayer run faster on my crappy S3 ProsavageDDR...

GullRaDriel
Member #3,861
September 2003
avatar

Quote:

Now, if I can only make OpenLayer run faster on my crappy S3 ProsavageDDR...

You can not ask a donkey to run as fast as a horse.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

raycrasher
Member #6,683
December 2005

Oh well. New card, then. Or strap a rocket booster to teh donkey.

Go to: