circular dependencies and typedefs

D

Dylan

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
 
D

David Harmon

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

Dylan

On Wed, 07 Jul 2004 16:12:55 +0100 in comp.lang.c++, Dylan


(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
 
H

Howard

Dylan said:
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
 
D

Dylan

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
 
J

Jeff Flinn

Dylan said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top