Jump to content
  • Advertisement

Archived

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

MV

c++ cross-references

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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...

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Thx for your help !

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!