Jump to content
  • Advertisement
Sign in to follow this  

C++ dependency problem

This topic is 4504 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.h

class B;

class A
{
private:
B* m_pYouCanOnlyHavePointersToBHere;
}





// A.cpp

#include A.h
#include B.h

// Static instances of B can be declared in the cpp

B StaticB;




Luck!

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
^^ 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!