Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   circular dependencies and typedefs (http://www.velocityreviews.com/forums/t284219-circular-dependencies-and-typedefs.html)

Dylan 07-07-2004 03:12 PM

circular dependencies and typedefs
 
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

David Harmon 07-07-2004 05:09 PM

Re: circular dependencies and typedefs
 
On Wed, 07 Jul 2004 16:12:55 +0100 in comp.lang.c++, Dylan
<spaldeen@ontheball.com> 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.


Dylan 07-07-2004 05:58 PM

Re: circular dependencies and typedefs
 
On Wed, 07 Jul 2004 17:09:18 GMT, David Harmon
<source@netcom.com.invalid> wrote:

>On Wed, 07 Jul 2004 16:12:55 +0100 in comp.lang.c++, Dylan
><spaldeen@ontheball.com> 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

Howard 07-07-2004 05:59 PM

Re: circular dependencies and typedefs
 

"Dylan" <spaldeen@ontheball.com> wrote in message
news:4ceoe0lrejed72tnrl0g1ueag60cj93uu8@4ax.com...
> On Wed, 07 Jul 2004 17:09:18 GMT, David Harmon
> <source@netcom.com.invalid> wrote:
>
> >On Wed, 07 Jul 2004 16:12:55 +0100 in comp.lang.c++, Dylan
> ><spaldeen@ontheball.com> 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





Dylan 07-07-2004 06:12 PM

Re: circular dependencies and typedefs
 
<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

Jeff Flinn 07-07-2004 06:39 PM

Re: circular dependencies and typedefs
 

"Dylan" <spaldeen@ontheball.com> wrote in message
news:i2foe05gjbn9mcjkhcng3dtusrsp4d1ric@4ax.com...
> <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 07-07-2004 06:54 PM

Re: circular dependencies and typedefs
 

"Jeff Flinn" <NONONE@nowhere.com> wrote in message
news:cchg0t$jis$1@bluegill.adi.com...
>
> "Dylan" <spaldeen@ontheball.com> wrote in message
> news:i2foe05gjbn9mcjkhcng3dtusrsp4d1ric@4ax.com...
> > <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

>
>




Dylan 07-07-2004 06:59 PM

Re: circular dependencies and typedefs
 
On Wed, 7 Jul 2004 14:39:23 -0400, "Jeff Flinn" <NONONE@nowhere.com>
wrote:

>
>"Dylan" <spaldeen@ontheball.com> wrote in message
>news:i2foe05gjbn9mcjkhcng3dtusrsp4d1ric@4ax.com.. .
>> <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




All times are GMT. The time now is 08:26 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.