Dependencies problem

This is a discussion on Dependencies problem within the C++ Programming forums, part of the General Programming Boards category; I have a header problem, when trying to #include 2 different files. Here's my problem: Header A: Code: #ifndef HEADER_A ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    28

    Dependencies problem

    I have a header problem, when trying to #include 2 different files. Here's my problem:

    Header A:

    Code:
    #ifndef HEADER_A
    #define HEADER_A
    
    #include "headerB.h"
    
    <class function definitions go here>
    <one function requires class structure from header b>
    
    #endif
    Header B:

    Code:
    #ifndef HEADER_B
    #define HEADER_B
    
    #include "headerA.h"
    
    <class function definitions go here>
    <one function requires class structure from header a>
    
    #endif
    Now, when I do this, header A tries to include B, but B needs to include header A first. But thats fine, there won't be any redefinition of functions problem, because I put the #ifndef HEADER_* in there. But what is a problem is that Header A never includes the class structure from Header B, because before it can read its class structure, it has to include A because it needs to know its class structure for the one function that requires that information. So the compiler completely skips the class structure in B, preventing the class structure in A from being defined. So my code won't compile.

    Any suggestions on how to get around this, if there is a way?
    -Grunt (Malek)

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    There are several solutions. However, I want to know your overall design in terms of objects. What is A and B? Your current design is not optimized because there is a circular relationship.

    Post your design in terms of objects.

    Kuphryn

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Yeah, you're in a trap...

    You'll have to re-structure. What you are saying is: A requires B, which requires B first, which requires A first...

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: Yeah, you're in a trap...

    Originally posted by DougDbug
    You'll have to re-structure. What you are saying is: A requires B, which requires B first, which requires A first...
    not nessicarily.

    He didn't say they encapsulated each other or that they recursively constructed each other. It's perfectly fine (and often necissary) to need data about both. If you only need a pointer to an instance of the class, then you dont need the class definition, just a forward declaration. If you need to use specific functions/datamembers, put all of the implementation in a cpp and include both headers in the cpp.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    28

    Dependencies

    Here's some sample code I just made up, I think it points out my problem. Any way to make this compile? Thanks.

    Header: stuff.h

    Code:
    #ifndef HEADER_STUFF
    #define HEADER_STUFF
    
    #include "things.h"
    
    class Stuff
    {
    	public:
    		void rar(int blah);
    		Things* giveMe(void);
    		int other(void);
    
    	private:
    		int x;
    		Things *theThing;
    };
    
    #endif
    Header: things.h

    Code:
    #ifndef HEADER_THINGS
    #define HEADER_THINGS
    
    #include "stuff.h"
    
    class Things
    {
    	public:
    		void doSomething(void);
    		Stuff* getStuff(void);
    		char* getText(void);
    
    	private:
    		char text;
    		Stuff *theStuff;
    };
    
    #endif
    Last edited by Malek; 01-17-2003 at 08:22 PM.
    -Grunt (Malek)

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    35
    Can't you put all your class declarations in the same place?

    I only see a problem when you're constructing these objects. But that's not a problem if you create the objects before you link them.

  7. #7
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    Can someone who uses Borland tell us what the #pragma or other directives are included by default in Borland. This may be the solution...

    #pragma once //Put this in your Header Files and see if it solves it.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Nono! You don't have to do either of those things!

    Just use a forward declaration of the class!

    The only reason you normally have to include the entire definition of the class is because it has to know how large each object is and what its members are (when implementing).

    When you are strictly dealing with pointers/function arguments, etc. you don't need the class definition at all -- the ONLY thing you need is the class declaration (because the size of a pointer to any primitive nonfunction/nonmemberfunction datatype is always the same).

    Just put:

    class Stuff;

    at the top of the Things file and

    class Things;

    at the top of the Stuff file

    you don't have to #include their headers.

    After that, in the files with the implementation, include both headers and you'll have no problems.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    28

    Dependencies

    Alright, thanks. The

    class Things;

    class Stuff;

    in the files worked. Thanks for the help.
    -Grunt (Malek)

  10. #10
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    foward reference will solve the problem.. But it looks like you have deployed a bad design statergy......
    In System Alalysis and SET terms the coupling between the two modules seems very high which is not recomended... i.e the two modules seems to be dependedn on each other heavily... Thought this is normal for programmers... It is not recomended..


    For a better design approach you can do the following:-

    a) Try combining the 2 modules
    b) Try shifting the dependent sub modules the the other making it only dependent one way...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21