#### Archived

This topic is now archived and is closed to further replies.

# c++ cross-references

This topic is 5658 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''ve got a problem using several class knowing each others : class A { B *_b; C *_c; }; class B { A *_a; C *_c; } class C { A *_a; B *_b; }; each class is defined in its own .h file with #ifndef ... #define ... statements. with 2 classes, I know how to solve it ; for each class head file, you have to first include a header file that just contains the 2 classes prototypes ( class A; classB; }. But with 3 classes or more, it''s not so easy ; sometimes it works, sometimes not, and I dont understand why. The only solution I found is to define all classes in a single file, and to first write prototypes, but it''s really dirty ! I''ve been struggling with this trouble for months !!! Please help !!!

##### Share on other sites
Just put the forward declarations before the class definition that uses them (in the same header file):
(in a.h)class B;class C;class A {B *_b;C *_c;};(in b.h)class A;class C;class B {A *_a;C *_c;}etc...

##### Share on other sites
I did it, but it doesn''t work in the case your header file contains a method or a member of the class.

class B;

class A {
A( B *_b) { a = _b->b };
int a;
};

class B {
int b;
};

in my case, I was dealing with an inline function (for performances), and you have to write it in the header file.

##### Share on other sites
in that case you must put the functions on a cpp file and there include the b.h

just do:

class B;

class A {
A( B *_b);
int a;
};

class B {
int b;
};

//A.cpp//
#include "B.h"
inline A::A( B *_b)
{
a = _b->b
}

To be considered a genius you just have to say what everybody knows in a way very few understand

##### Share on other sites
Sur you can do this ?
I think i tried, but the compiler "told" me i had to put inline functions in the header file.

##### Share on other sites
You''re rigth, it''s ok !
Thx !!!

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633723
• Total Posts
3013556
×