Thread: Circular dependency / Compile Error

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    26

    Circular dependency / Compile Error

    Hello all!
    I'm trapped trying to compile the following (suposing) code.
    I have a class A that uses B objects. And the class B uses A objects.

    The compiler( Bcc32.exe ) generates an error because, while trying to compile A.cpp, it reads the A.h
    A.h has the "#include B.h", so it reads B.h
    B.h has the "include A.h", but it doesn't read it again, parsing the file until it gets the "void doThat( A& a )". There is a object "a" of the class "A" that wasn't (full) parsed yet, so it prints something like:

    error: " ")" expected"

    /*--------------class A----------------*/

    //File A.h

    #include "B.h"

    class A
    {
    public:
    void set( B b );
    };

    //----EOF---

    //file A.cpp

    #include "A.h"
    void A::set( B b ) { /* code */ }

    //----EOF---

    /*--------------class B----------------*/

    //file B.h

    #include "A.h"

    class B
    {
    public:
    void doThat( A& a );
    };

    //----EOF----

    //file B.cpp

    #include "B.h"

    void B::doThat( A& a ) { /* code */ }

    //----EOF---


    Thanks
    Jester
    #pwd
    Brazil

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    1) Don't include a.h within b.h or b.h within a.h. You don't need that. (See below)

    2) You should make sure no header can be included more than once during any compile. MSVC++ allows you to use the #pragma once directive, or you can do it the old fashioned way (see below):


    Code:
    // within A.H
    
    
    #ifndef _A_H_43756436598765439347
    #define _A_H_43756436598765439347
    // we use the filename and a random string of numbers to guarantee a unique symbol
    
    class B;  // we tell it that there exists some class B somewhere.
    
    // rest of A.h
    
    #endif  // don't forget this
    Also, both source files should include both headers.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    26
    Thanks for the reply (this was a fast one)

    The #include guards I have done. The problem is not a double inclusion, but the fact that for both classes to compile, the other has to be compiled first. So the pre-compiler gets angry and none is compiled.


    I belive this would solve the problem like you said:
    "class B;"

    But Borland compiler doesn't accept this statement.
    If it does, I couldn't figure the option to do so.
    #pwd
    Brazil

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by jester
    Thanks for the reply (this was a fast one)

    The #include guards I have done. The problem is not a double inclusion, but the fact that for both classes to compile, the other has to be compiled first. So the pre-compiler gets angry and none is compiled.


    I belive this would solve the problem like you said:
    "class B;"

    But Borland compiler doesn't accept this statement.
    If it does, I couldn't figure the option to do so.
    It might be because you are declaring data members like this:

    B myBMember;

    Try using pointers instead:

    B * myBPointer;

    I think the reason is, when it tries to allocate space, it needs to know sizeof(A) to know sizeof(B), and sizeof(B) to know sizeof(A). There's also an infinite loop there -- B contains an object of type A, which contains an object of type B, which contains an object of type A, etc.

    All those problems will disappear when you use pointers. sizeof(B*) and sizeof(A*) are both always known, and you don't get into infinite loops.

    If you always use them as pointers, you shouldn't have compile issues.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    26
    All right!! thanks for the explanation!!
    #pwd
    Brazil

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  3. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM