Skip to main content
gamedev.net
🔒 Locked

C++ dependency problem

Started by manuelb Jan 20, 2006 at 5:15 AM 5 replies 700+ views
Original Post
manuelb
manuelb
I'm using dev-C++, and I have this problem: a.h depends on b.h and b.h depends on a.h It just can't compile. It's a C# project that I'm porting to C++, and it's not just 2 classes, a lot of classes has this problem. I'm not very experienced on makefiles so I'm using dev-c++ generated makefile, and I don't know what to do to solve this problem. What should I do? Thank's.
--------------------------Rory Gallagher and Chico Science music !!!!!!!---------------------------____||~~~~|/TT
owl
owl
You can't do that kind of mutual inclusion because it becomes recursive.

The closest thing to do is to forward declare class B in A.h.

// A.hclass B;class A{ private:  B* m_pYouCanOnlyHavePointersToBHere;}



// A.cpp#include A.h#include B.h// Static instances of B can be declared in the cppB StaticB;


Luck!
[size="2"]I like the Walrus best.
uncutno
uncutno
ok, this is impossible and should not compile!

Solution1:

a_interface.h
a.h
b.h

a.h depends on b.h
b.h depends on a_interface.h
a.h is an implementation of a_interface.h (if you use classes and inheritance)

Solution2:

a.cpp depends on a.h and b.h
b.cpp depends on a.h and b.h
a.h and b.h dont depend on eachother...

If you create a pointer to A in B, you can write
class A *a_pointer; , whitout including a.h!
Then you include a.h in b.cpp and place all your
usage og A in the b.cpp (assigning og a_pointer can be done in b.h)

This is good practice so that you avoid large dependancy trees of h files!
Also, add #pragma once at start(MSVS special) or:
#ifndef FILENAME_OF_A_OR_B
#define FILENAME_OF_A_OR_B

.... data

//at end of file:
#endif FILENAME_OF_A_OR_B

...to all your h files, so that they only load once!
(else you get "is already defined" errors)
-Anders-Oredsson-Norway-
ciroknight
ciroknight
^^ agree with him.

"a.h depends on b.h and b.h depends on a.h" is a Cyclic dependency; when the compiler looks at it, it would go on forever, loading one, then the other, then the first again. Instead, it detects the cyclic dependency error and stops compiling, returning some kind of error.

The solutions may vary depending on what's actually in a.h/.ccp and b.h/.cpp, so I can't help you there without more information, but that's what's wrong.
manuelb
manuelb
Of couse! That's so simple!:)
I think my brain got lazy working with C# last times. hehe.

Thank's a lot!
--------------------------Rory Gallagher and Chico Science music !!!!!!!---------------------------____||~~~~|/TT
manuelb
manuelb
By the way, owl's solution worked fine, I'm using "include" when it's an aggregation, and using forward declaration when its just a pointer relation, and works very well.
Thank's you all!

offtopic:
owl, can't wait for Brasil X Argentina in this soccer world cup! :) thank's again.
--------------------------Rory Gallagher and Chico Science music !!!!!!!---------------------------____||~~~~|/TT
owl
owl
Quote:
Original post by manuelb
offtopic:
owl, can't wait for Brasil X Argentina in this soccer world cup! :) thank's again.


I really don't like soccer, but that kind of classic is something nobody can miss, not even I. :)
[size="2"]I like the Walrus best.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.