circular dependencies and typedefs

Discussion in 'C++' started by Dylan, Jul 7, 2004.

  1. Dylan

    Dylan Guest

    Is there a way of declaring the following without making A::m_b a
    pointer or a reference?

    /******************* classA.h ****************** */
    #ifndef CLASSA
    #define CLASSA
    #include <list>


    class B;

    class A
    {
    public:

    typedef std::list<int> Container;

    B m_b;

    };

    #endif

    /******************* classB.h ****************** */

    #ifndef CLASSB
    #define CLASSB

    class A;

    class B
    {
    public:

    void (const A::Container& cont){}

    };

    #endif

    *******************************************

    Thanks
     
    Dylan, Jul 7, 2004
    #1
    1. Advertising

  2. Dylan

    David Harmon Guest

    On Wed, 07 Jul 2004 16:12:55 +0100 in comp.lang.c++, Dylan
    <> wrote,
    >Is there a way of declaring the following without making A::m_b a
    >pointer or a reference?


    (example snipped)

    Yes. Class A has a member B, so it needs #include "B.h" before the
    class A definition and not just a forward declaration of B.

    B has only indirect mention of A, so the forward declaration works
    there. Of course if B tried to contain a member A, you would have an
    impassability.
     
    David Harmon, Jul 7, 2004
    #2
    1. Advertising

  3. Dylan

    Dylan Guest

    On Wed, 07 Jul 2004 17:09:18 GMT, David Harmon
    <> wrote:

    >On Wed, 07 Jul 2004 16:12:55 +0100 in comp.lang.c++, Dylan
    ><> wrote,
    >>Is there a way of declaring the following without making A::m_b a
    >>pointer or a reference?

    >
    >(example snipped)
    >
    >Yes. Class A has a member B, so it needs #include "B.h" before the
    >class A definition and not just a forward declaration of B.
    >
    >B has only indirect mention of A, so the forward declaration works
    >there. Of course if B tried to contain a member A, you would have an
    >impassability.



    no, that doesn't work David. Not in MSVC2003 anyway
     
    Dylan, Jul 7, 2004
    #3
  4. Dylan

    Howard Guest

    "Dylan" <> wrote in message
    news:...
    > On Wed, 07 Jul 2004 17:09:18 GMT, David Harmon
    > <> wrote:
    >
    > >On Wed, 07 Jul 2004 16:12:55 +0100 in comp.lang.c++, Dylan
    > ><> wrote,
    > >>Is there a way of declaring the following without making A::m_b a
    > >>pointer or a reference?

    > >
    > >(example snipped)
    > >
    > >Yes. Class A has a member B, so it needs #include "B.h" before the
    > >class A definition and not just a forward declaration of B.
    > >
    > >B has only indirect mention of A, so the forward declaration works
    > >there. Of course if B tried to contain a member A, you would have an
    > >impassability.

    >
    >
    > no, that doesn't work David. Not in MSVC2003 anyway


    That's pretty vague. What do your files look like now, and what's the error
    you get?

    -Howard
     
    Howard, Jul 7, 2004
    #4
  5. Dylan

    Dylan Guest

    <snipped>
    >>
    >>
    >> no, that doesn't work David. Not in MSVC2003 anyway

    >
    >That's pretty vague. What do your files look like now, and what's the error
    >you get?
    >
    >-Howard
    >
    >
    >

    Here you go


    /******************* classA.h ****************** */
    #ifndef CLASSA
    #define CLASSA
    #include <list>
    #include "classB.h"

    class B;

    class A
    {
    public:

    typedef std::list<int> Container;

    B m_b;

    };

    #endif

    /******************* classB.h ****************** */

    #ifndef CLASSB
    #define CLASSB

    class A;

    class B
    {
    public:

    void (const A::Container& cont){}

    };

    #endif

    /**************** main.cpp ***************************/
    #include "classA.h"
    #include "classB.h"


    int main()
    {
    return 0;
    }

    *********************************************************

    This will give you the error messages

    Visual Studio Projects\another_temp\classB.h(10) : error C2027: use of
    undefined type 'A'
    Visual Studio Projects\another_temp\classB.h(4) : see
    declaration of 'A'
    Visual Studio Projects\another_temp\classB.h(10) : error C2062: type
    'const int' unexpected
    Visual Studio Projects\another_temp\classB.h(10) : error C2334:
    unexpected token(s) preceding '{'; skipping apparent function body
     
    Dylan, Jul 7, 2004
    #5
  6. Dylan

    Jeff Flinn Guest

    "Dylan" <> wrote in message
    news:...
    > <snipped>
    > >>
    > >>
    > >> no, that doesn't work David. Not in MSVC2003 anyway

    > >
    > >That's pretty vague. What do your files look like now, and what's the

    error
    > >you get?
    > >
    > >-Howard
    > >
    > >
    > >

    > Here you go
    >
    >
    > /******************* classA.h ****************** */
    > #ifndef CLASSA
    > #define CLASSA
    > #include <list>
    > #include "classB.h"
    >
    > class B;
    >
    > class A
    > {
    > public:
    >
    > typedef std::list<int> Container;
    >
    > B m_b;
    >
    > };
    >
    > #endif
    >
    > /******************* classB.h ****************** */
    >
    > #ifndef CLASSB
    > #define CLASSB
    >
    > class A;
    >
    > class B
    > {
    > public:
    >
    > void (const A::Container& cont){}


    Huh???

    >
    > };
    >
    > #endif
    >
    > /**************** main.cpp ***************************/
    > #include "classA.h"
    > #include "classB.h"
    >
    >
    > int main()
    > {
    > return 0;
    > }
    >
    > *********************************************************
    >
    > This will give you the error messages
    >
    > Visual Studio Projects\another_temp\classB.h(10) : error C2027: use of
    > undefined type 'A'
    > Visual Studio Projects\another_temp\classB.h(4) : see
    > declaration of 'A'
    > Visual Studio Projects\another_temp\classB.h(10) : error C2062: type
    > 'const int' unexpected
    > Visual Studio Projects\another_temp\classB.h(10) : error C2334:
    > unexpected token(s) preceding '{'; skipping apparent function body
     
    Jeff Flinn, Jul 7, 2004
    #6
  7. Dylan

    Jeff Flinn Guest

    "Jeff Flinn" <> wrote in message
    news:cchg0t$jis$...
    >
    > "Dylan" <> wrote in message
    > news:...
    > > <snipped>
    > > >>
    > > >>
    > > >> no, that doesn't work David. Not in MSVC2003 anyway
    > > >
    > > >That's pretty vague. What do your files look like now, and what's the

    > error
    > > >you get?
    > > >
    > > >-Howard
    > > >
    > > >
    > > >

    > > Here you go
    > >
    > >
    > > /******************* classA.h ****************** */
    > > #ifndef CLASSA
    > > #define CLASSA
    > > #include <list>
    > > #include "classB.h"
    > >
    > > class B;
    > >
    > > class A
    > > {
    > > public:
    > >
    > > typedef std::list<int> Container;


    Move this typedef out of A.


    > >
    > > B m_b;
    > >
    > > };
    > >
    > > #endif
    > >
    > > /******************* classB.h ****************** */
    > >
    > > #ifndef CLASSB
    > > #define CLASSB
    > >
    > > class A;
    > >
    > > class B
    > > {
    > > public:
    > >
    > > void (const A::Container& cont){}

    >
    > Huh???
    >
    > >
    > > };
    > >
    > > #endif
    > >
    > > /**************** main.cpp ***************************/
    > > #include "classA.h"
    > > #include "classB.h"
    > >
    > >
    > > int main()
    > > {
    > > return 0;
    > > }
    > >
    > > *********************************************************
    > >
    > > This will give you the error messages
    > >
    > > Visual Studio Projects\another_temp\classB.h(10) : error C2027: use of
    > > undefined type 'A'
    > > Visual Studio Projects\another_temp\classB.h(4) : see
    > > declaration of 'A'
    > > Visual Studio Projects\another_temp\classB.h(10) : error C2062: type
    > > 'const int' unexpected
    > > Visual Studio Projects\another_temp\classB.h(10) : error C2334:
    > > unexpected token(s) preceding '{'; skipping apparent function body

    >
    >
     
    Jeff Flinn, Jul 7, 2004
    #7
  8. Dylan

    Dylan Guest

    On Wed, 7 Jul 2004 14:39:23 -0400, "Jeff Flinn" <>
    wrote:

    >
    >"Dylan" <> wrote in message
    >news:...
    >> <snipped>
    >> >>
    >> >>
    >> >> no, that doesn't work David. Not in MSVC2003 anyway
    >> >
    >> >That's pretty vague. What do your files look like now, and what's the

    >error
    >> >you get?
    >> >
    >> >-Howard
    >> >
    >> >
    >> >

    >> Here you go
    >>
    >>
    >> /******************* classA.h ****************** */
    >> #ifndef CLASSA
    >> #define CLASSA
    >> #include <list>
    >> #include "classB.h"
    >>
    >> class B;
    >>
    >> class A
    >> {
    >> public:
    >>
    >> typedef std::list<int> Container;
    >>
    >> B m_b;
    >>
    >> };
    >>
    >> #endif
    >>
    >> /******************* classB.h ****************** */
    >>
    >> #ifndef CLASSB
    >> #define CLASSB
    >>
    >> class A;
    >>
    >> class B
    >> {
    >> public:
    >>
    >> void (const A::Container& cont){}

    >
    >Huh???


    oops! Change that to:

    void DoSomething(const A::Container& cont){}

    You'll still get the error
     
    Dylan, Jul 7, 2004
    #8
    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. Suzanne Vogel
    Replies:
    2
    Views:
    2,631
    Suzanne Vogel
    Jun 26, 2003
  2. crichmon
    Replies:
    3
    Views:
    778
    crichmon
    Jun 28, 2004
  3. ernesto basc?n pantoja

    Circular dependencies

    ernesto basc?n pantoja, Nov 29, 2004, in forum: C++
    Replies:
    2
    Views:
    3,789
    Larry Brasfield
    Nov 29, 2004
  4. Kiuhnm
    Replies:
    16
    Views:
    757
    Jonathan Mcdougall
    Jan 3, 2005
  5. Erik Veenstra
    Replies:
    12
    Views:
    200
    Andrew Goifeld
    Sep 19, 2008
Loading...

Share This Page