c++ cross-references

Started by
4 comments, last by MV 20 years, 10 months ago
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 !!!
Advertisement
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...
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.
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
To be considered a genius you just have to say what everybody knows in a way very few understand
Sur you can do this ?
I think i tried, but the compiler "told" me i had to put inline functions in the header file.

Thx for your help !
You''re rigth, it''s ok !
Thx !!!

This topic is closed to new replies.

Advertisement