How to include header files ?

Discussion in 'C++' started by vivekian, Nov 27, 2005.

  1. vivekian

    vivekian Guest

    Have a program where an object A uses a pointer to object B in a.cpp
    and object B uses object A in b.cpp. The declarations have been put in
    a.h and b.h respectively. Now if i include a.h and b.h both in a.cpp
    and b.cpp , i get 'previously defined here' and 'redifination' errors.
    What is the solution to this ?

    Thanks,
    vivekian
    vivekian, Nov 27, 2005
    #1
    1. Advertising

  2. * vivekian:
    > Have a program where an object A uses a pointer to object B in a.cpp
    > and object B uses object A in b.cpp. The declarations have been put in
    > a.h and b.h respectively. Now if i include a.h and b.h both in a.cpp
    > and b.cpp , i get 'previously defined here' and 'redifination' errors.
    > What is the solution to this ?


    There are many solutions, not one.

    FAQ item 39.11 gives a purely technical answer, perhaps the simplest, a
    forward-declaration.
    <url:
    http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.11>.

    struct A;
    struct B;

    struct A { B* myB; };
    struct B { A* myA; };

    It's important to know about forward declarations.

    But an often more clean solution is the abstract class solution:

    struct AbstractA { ... };
    struct AbstractB { ... };
    struct A: AbstractA { AbstractB* myB; };
    struct B: AbstractB { AbstractA* myA; }

    This solution helps you factor out what's really needed for A and B to
    do their work, i.e. it solves the design-level problem instead of just
    alleviating the immediate C++ symptom of the problem.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 27, 2005
    #2
    1. Advertising

  3. vivekian

    vivekian Guest

    Alf P. Steinbach wrote :
    >There are many solutions, not one.


    >FAQ item 39.11 gives a purely technical answer, perhaps the simplest, a
    >forward-declaration.
    ><url:
    >http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.11>.


    > struct A;
    > struct B;


    > struct A { B* myB; };
    > struct B { A* myA; };


    >It's important to know about forward declarations.



    Not too sure about this , but i think by including those header files
    a.h and b.h in both a.cpp and b.cpp i do forward declare them . The
    problem i am facing not circular dependency but rather much more of
    redeclaration compile time errors i.e.

    // sample code - a.cpp

    #include b.h
    #include a.h

    /*..........*/

    //sample code - b.cpp

    #include a.h
    #include b.h
    vivekian, Nov 27, 2005
    #3
  4. * vivekian:
    > Alf P. Steinbach wrote :
    > >There are many solutions, not one.

    >
    > >FAQ item 39.11 gives a purely technical answer, perhaps the simplest, a
    > >forward-declaration.
    > ><url:
    > >http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.11>.

    >
    > > struct A;
    > > struct B;

    >
    > > struct A { B* myB; };
    > > struct B { A* myA; };

    >
    > >It's important to know about forward declarations.

    >
    >
    > Not too sure about this , but i think by including those header files
    > a.h and b.h in both a.cpp and b.cpp i do forward declare them . The
    > problem i am facing not circular dependency but rather much more of
    > redeclaration compile time errors i.e.
    >
    > // sample code - a.cpp
    >
    > #include b.h
    > #include a.h
    >
    > /*..........*/
    >
    > //sample code - b.cpp
    >
    > #include a.h
    > #include b.h


    Uh, well, then you just haven't come to the dependency problem yet.

    For the redefinition problem just add proper #include guards to the
    header files, like this:

    #ifndef A_H
    #define A_H
    // ... contents of [a.h]
    #endif

    For more detailed information see section 6 through 10 of chapter
    (whatever) 1.6 of the attempted "Correct C++ Tutorial", at
    <url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01.html>.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 27, 2005
    #4
  5. vivekian

    vivekian Guest

    Alf P. Steinbach wrote:

    > Uh, well, then you just haven't come to the dependency problem yet.
    >
    > For the redefinition problem just add proper #include guards to the
    > header files, like this:
    >
    > #ifndef A_H
    > #define A_H
    > // ... contents of [a.h]
    > #endif
    >
    > For more detailed information see section 6 through 10 of chapter
    > (whatever) 1.6 of the attempted "Correct C++ Tutorial", at
    > <url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01.html>.
    >


    Alf,

    Thanks. Things much more clear now .
    vivekian, Nov 27, 2005
    #5
  6. > Have a program where an object A uses a pointer to object B in a.cpp
    > and object B uses object A in b.cpp. The declarations have been put in
    > a.h and b.h respectively. Now if i include a.h and b.h both in a.cpp
    > and b.cpp , i get 'previously defined here' and 'redifination' errors.
    > What is the solution to this ?


    The following article should help:

    http://www.eventhelix.com/RealtimeMantra/HeaderFileIncludePatterns.htm

    --
    EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
    Sequence Diagram Based System Design and Object Modeling Tool
    EventHelix.com, Nov 27, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aguilar, James
    Replies:
    2
    Views:
    685
    Aguilar, James
    Jul 16, 2004
  2. John Smith

    Header files included in header files

    John Smith, Jul 21, 2004, in forum: C Programming
    Replies:
    18
    Views:
    603
    Jack Klein
    Jul 24, 2004
  3. Replies:
    3
    Views:
    1,298
    Walter Roberson
    May 1, 2006
  4. Andreas Bogenberger
    Replies:
    3
    Views:
    902
    Andreas Bogenberger
    Feb 22, 2008
  5. mlt
    Replies:
    2
    Views:
    831
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page