Cross-Inclusion of C++ classes

Hello,
Structure of program is as follows:

A.h
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef A_H
#define A_H

#include "B.h"

class A
{
	public:
			A(B);
};

#endif 


B.h
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef B_H
#define B_H

#include "A.h"

class B
{
   public:
	B(A);
};

#endif 


The error that i get is :-

A.cpp
c:\b.h(9) : error C2460: 'A' : uses 'B', which is being defined
c:\b.h(7) : see declaration of 'B'
B.cpp
c:\a.h(9) : error C2460: 'B' : uses 'A', which is being defined
c:\a.h(7) : see declaration of 'A'

In one of the earlier posts got to know that by those ifndef statements duplicate inclusion of files is taken care of.

How does one remove this kind of errors?

help appreciated
amal
Try moving your constructor over to the .cpp file, that should work.
You will need forward declarations.

[code]
// A.h
#ifndef A_H
#define A_H

// Forward declare B
class B;

class A {
explicit A( const B& );
};

#endif

However, it is a bit odd to have class A be constructible from a B and B constructible from an A, as it seems that A is a B and B is an A (so are they not then the same thing?)
IMO. That should cause an infinite loop trying to declare one of those.

Design problem IMO.
Topic archived. No new replies allowed.