How to declare interdependent classes in different header files

A

Andre

Is it possible to declare interdependent classes in different header
files?

If so, how could the following errors in the following piece of code
be corrected?

Thanks in advance,

Andre

1>c:\users\andre\_my\tmp\test01\test01\Struct_B.h(10): error C2146:
syntax error : missing ';' before identifier 'mStructA'
1>c:\users\andre\_my\tmp\test01\test01\Struct_B.h(10): error C4430:
missing type specifier - int assumed. Note: C++ does not support
default-int
1>c:\users\andre\_my\tmp\test01\test01\Struct_B.h(10): error C4430:
missing type specifier - int assumed. Note: C++ does not support
default-int
1>c:\users\andre\_my\tmp\test01\test01\Struct_A.h(8): error C2236:
unexpected 'class' 'Struct_A'. Did you forget a ';'?
1>c:\users\andre\_my\tmp\test01\test01\Struct_A.h(8): error C2143:
syntax error : missing ';' before '{'
1>c:\users\andre\_my\tmp\test01\test01\Struct_A.h(8): error C2447:
'{' : missing function header (old-style formal list?)

In Struct_A.hpp:

#ifndef STRUCT_A
#define STRUCT_A

#include "Struct_B.h"

class Struct_A
{
public:
Struct_B mStructB;
}

#endif // STRUCT_A

In Struct_B.hpp:

#ifndef STRUCT_B
#define STRUCT_B

#include "Struct_A.h"

class Struct_B
{
public:
Struct_A mStructA;
}

#endif // STRUCT_B
 
A

Andre

Is it possible to declare interdependent classes in different header
files?

If so, how could the following errors in the following piece of code
be corrected?

Thanks in advance,

Andre

1>c:\users\andre\_my\tmp\test01\test01\Struct_B.h(10): error C2146:
syntax error : missing ';' before identifier 'mStructA'
1>c:\users\andre\_my\tmp\test01\test01\Struct_B.h(10): error C4430:
missing type specifier - int assumed. Note: C++ does not support
default-int
1>c:\users\andre\_my\tmp\test01\test01\Struct_B.h(10): error C4430:
missing type specifier - int assumed. Note: C++ does not support
default-int
1>c:\users\andre\_my\tmp\test01\test01\Struct_A.h(8): error C2236:
unexpected 'class' 'Struct_A'. Did you forget a ';'?
1>c:\users\andre\_my\tmp\test01\test01\Struct_A.h(8): error C2143:
syntax error : missing ';' before '{'
1>c:\users\andre\_my\tmp\test01\test01\Struct_A.h(8): error C2447:
'{' : missing function header (old-style formal list?)

In Struct_A.hpp:

#ifndef STRUCT_A
#define STRUCT_A

#include "Struct_B.h"

class Struct_A
{
public:
   Struct_B mStructB;

}

#endif // STRUCT_A

In Struct_B.hpp:

#ifndef STRUCT_B
#define STRUCT_B

#include "Struct_A.h"

class Struct_B
{
public:
   Struct_A mStructA;

}

#endif // STRUCT_B

Actually I made a mistake when declaring the classes. Please see
attached error messages and code

1>c:\users\andre\_my\tmp\test01\test01\Struct_B.h(10): error C2146:
syntax error : missing ';' before identifier 'mStructA'
1>c:\users\andre\_my\tmp\test01\test01\Struct_B.h(10): error C4430:
missing type specifier - int assumed. Note: C++ does not support
default-int
1>c:\users\andre\_my\tmp\test01\test01\Struct_B.h(10): error C4430:
missing type specifier - int assumed. Note: C++ does not support
default-int

In Struct_A.hpp:

#ifndef STRUCT_A
#define STRUCT_A

#include "Struct_B.h"

class Struct_A
{
public:
Struct_B mStructB;
};

#endif // STRUCT_A

In Struct_B.hpp:

#ifndef STRUCT_B
#define STRUCT_B

#include "Struct_A.h"

class Struct_B
{
public:
Struct_A mStructA;
};

#endif // STRUCT_B
 
J

John Bode

Is it possible to declare interdependent classes in different header
files?

[snip]


Actually I made a mistake when declaring the classes. Please see
attached error messages and code

1>c:\users\andre\_my\tmp\test01\test01\Struct_B.h(10): error C2146:
syntax error : missing ';' before identifier 'mStructA'
1>c:\users\andre\_my\tmp\test01\test01\Struct_B.h(10): error C4430:
missing type specifier - int assumed. Note: C++ does not support
default-int
1>c:\users\andre\_my\tmp\test01\test01\Struct_B.h(10): error C4430:
missing type specifier - int assumed. Note: C++ does not support
default-int

In Struct_A.hpp:

#ifndef STRUCT_A
#define STRUCT_A

#include "Struct_B.h"

class Struct_A
{
public:
   Struct_B mStructB;

};

#endif // STRUCT_A

In Struct_B.hpp:

#ifndef STRUCT_B
#define STRUCT_B

#include "Struct_A.h"

class Struct_B
{
public:
   Struct_A mStructA;

};

#endif // STRUCT_B

You cannot have circular dependencies where type B has an instance of
type A and vice versa (this is equally true in C and C++). What you
can do is create incomplete type definitions for B and have A declare
a pointer (or in C++, a reference) to it and vice versa:

structA.h:
/**
* This example is C-based, but the same concept holds true
* for classes in C++
*/
#ifndef STRUCT_A
#define STRUCT_A

struct b; /* incomplete type definition */

struct a
{
struct b *bptr;
...
};

struct a afoo(struct b *ptr);
#endif

structB.h:

#ifndef STRUCT_B
#define STRUCT_B

struct a; /* incomplete type definition */

struct b
{
struct a *aptr;
...
};

struct b bfoo(struct a *ptr);
#endif

Note that neither header imports the other; instead, you defer that to
your implementation files, which will import both headers:

structA.c:

#include "structA.h"
#include "structB.h"

struct a afoo(struct b *ptr)
{
struct a newfoo;
newfoo.bptr = ptr;
return newfoo;
}

structB.c:

#include "structB.h"
#include "structA.h"

struct b bfoo(struct a *ptr)
{
struct b newfoo;
newfoo.aptr = ptr;
return newfoo;
}

Circular dependencies are bad juju and should be avoided whenever
possible. If you need to associate an A with a B or vice versa, it
might be better to create a third type to handle the mapping:

structC.h:

#ifndef STRUCT_C
#define STRUCT_C

#include "structA.h"
#include "structB.h"

struct c {
struct a m_a;
struct b m_b;
};

....
 

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,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top