Results 1 to 9 of 9

Thread: Circular references in C/C++ headers

  1. #1
    Join Date
    Apr 2007
    Beans
    116

    Circular references in C/C++ headers

    I asked a couple of weeks back about problems with infinite loops in the compiler caused by 2 C++ classes having references to each other's header files and was pointed in the direction of using

    #ifndef H_DDRoute
    #define H_DDRoute
    //content of header here
    #endif

    This sure enough solved the infinite loop but Im getting errors from the compiler about classes being undefined which just doesnt make sense to me.

    For example my program has 4 classes in total all of which are very simple but have references to each other.

    The class diagram is here www2.simple.org/classes.pdf

    The class Exchange compiles fine. The class's DDRoute, SnapshotCard and ServiceCode dont.

    Taking DDRoute as an example, you'll see on the class diagram it has a pointer to Exchange and a pointer to SnapshotCard. SnapshotCard also has a pointer to DDRoute.

    The header file for DDRoute is

    #ifndef H_DDRoute
    #define H_DDRoute

    #include "Exchange.h"
    #include "SnapshotCard.h"

    using namespace std;

    class DDRoute
    {

    protected:
    int id;
    int code;
    Exchange* dest_exch;
    SnapshotCard* snapshot;


    public:
    DDRoute();
    ~DDRoute();
    int getID();
    void setID(int pk);
    int getCode();
    void setCode(int c);
    Exchange* getDestinationExchange();
    void setDestinationExchange(Exchange* e);
    SnapshotCard* getSnapshotCard();
    void setSnapshotCard(SnapshotCard* card);



    };

    #endif

    When compiled GCC says:



    andrew@p4-gw2k-black:~/Desktop/thg$ g++ ./DDRoute.cpp -c -I .
    In file included from SnapshotCard.h:9,
    from DDRoute.h:5,
    from ./DDRoute.cpp:1:
    ServiceCode.h:17: syntax error before `*'
    ServiceCode.h:27: syntax error before `*'
    ServiceCode.h:28: `SnapshotCard' was not declared in this scope
    ServiceCode.h:28: `card' was not declared in this scope
    ServiceCode.h:28: variable or field `setSnapshotCard' declared void
    In file included from DDRoute.h:5,
    from ./DDRoute.cpp:1:
    SnapshotCard.h:24: `DDRoute' was not declared in this scope
    SnapshotCard.h:24: parse error before `>'
    SnapshotCard.h:41: parse error before `::'
    SnapshotCard.h:45: `DDRoute' was not declared in this scope
    SnapshotCard.h:45: parse error before `>'
    SnapshotCard.h:46: `DDRoute' was not declared in this scope
    SnapshotCard.h:46: `ddr' was not declared in this scope
    SnapshotCard.h:46: variable or field `addDDRoute' declared void


    I cannot see a reason for any of these errors. Its been given the headers for the other classes so why are they 'undefined' and also why is it moaning about methods that return void values.

    The headers for the other 2 classes are at

    www2.simple.org/SnapshotCard.txt

    www2.simple.org/ServiceCode.txt


    I appreciate I've given you a lot of stuff here but if anyone can help me fathom this out (im sure its something basic ive done wrong) I'd be happy to compensate your time through PayPal.

    Thanks
    Andrew

  2. #2
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Circular references in C/C++ headers

    Use a forward declaration.

    Quick Google search: http://www-subatech.in2p3.fr/~photon...PP-INC-1.shtml

    Hope it helps!

  3. #3
    Join Date
    Sep 2007
    Location
    Sweden
    Beans
    25
    Distro
    Gutsy Gibbon Testing

    Re: Circular references in C/C++ headers

    You need to forward declare the classes.

    Code:
    #ifndef H_DDRoute
    #define H_DDRoute
    
    #include "Exchange.h"
    #include "SnapshotCard.h"
    
    using namespace std;
    
    //FORWARD DECLARATION BELOW
    class SnapshotCard;
    
    class DDRoute
    {
    
    protected:
    int id;
    int code;
    Exchange* dest_exch;
    SnapshotCard* snapshot;
    
    
    public:
    DDRoute();
    ~DDRoute();
    int getID();
    void setID(int pk);
    int getCode();
    void setCode(int c);
    Exchange* getDestinationExchange();
    void setDestinationExchange(Exchange* e);
    SnapshotCard* getSnapshotCard();
    void setSnapshotCard(SnapshotCard* card);
    
    
    
    };
    
    #endif
    (i might be wrong has been some time since i coded in c++ now)
    //Tripokey

  4. #4
    Join Date
    May 2007
    Location
    Paris, France
    Beans
    927
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: Circular references in C/C++ headers

    As the others said, you need to use forward references.


    But... really, 4 classes all referencing each other is usually a symptom of a bigger problem. Perhaps you may want to rethink your design in order to get rid of some of the dependencies?
    Not even tinfoil can save us now...

  5. #5
    Join Date
    Apr 2007
    Beans
    116

    Re: Circular references in C/C++ headers

    spot on thanks

  6. #6
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Circular references in C/C++ headers

    Not related, but remove the "using namespace std" from the header file(s). In the one example you provided, not a single data type is from the "std" namespace.

    Rule of thumb: Never put a "using namespace" statement in a header file.

  7. #7
    Join Date
    Sep 2007
    Location
    Sintra
    Beans
    28
    Distro
    Ubuntu 5.10

    Re: Circular references in C/C++ headers

    Quote Originally Posted by aks44 View Post
    As the others said, you need to use forward references.


    But... really, 4 classes all referencing each other is usually a symptom of a bigger problem. Perhaps you may want to rethink your design in order to get rid of some of the dependencies?
    Sure that seems spaghetti code, try to redefine your design, and group things that seem related in an common class. Also looking at your design, isn't SnapshotCard class with too many methods? Also if SnapshotCard already has an DDRoute and ServiceCode vector in his field, why does two classes need an reference to his parent class? If they've accessed only by SnapshotCard calls, the caller always knows, that an specific DDRout object and ServiceCode object, belong to an specific SnapshotCard.

  8. #8
    Join Date
    Jan 2012
    Beans
    1

    Talking Re: Circular references in C/C++ headers

    Quote Originally Posted by LaRoza View Post
    Use a forward declaration.

    Quick Google search: http://www-subatech.in2p3.fr/~photons/subatech/soft/carnac/CPP-INC-1.shtml

    Hope it helps!
    Hey guy, thank you for your reference to the forward declaration, which really helps.


  9. #9
    Join Date
    Oct 2006
    Beans
    58,286

    Re: Circular references in C/C++ headers

    Thread closed.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •