468,484 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

Problem : cross reference between classes

I have two classes , say A and B. A contains methods named f1() ,
f2() and B contains methods named g1(), g2().
f1() will invoke g2() and g1() will invoke f2(). This situation is
kinda like two things interact with each other.
But the program couldn't pass the compliation. g++ says " invalid use
of undefined type of ......" .
As far as I know, forward declaration couldn't solve this problem, and
I have tried. Both of the definition of the two classes has to come
first which is not possible.
This can be solved by redesign A and B, but I wish I can find some
other solutions here, any advice will be welcome .
Dec 31 '07 #1
3 5125
xd*****@gmail.com wrote:
I have two classes , say A and B. A contains methods named f1() ,
f2() and B contains methods named g1(), g2().
f1() will invoke g2() and g1() will invoke f2(). This situation is
kinda like two things interact with each other.
This smells like some design problem. What object from where
is to be accessed?
But the program couldn't pass the compliation. g++ says " invalid use
of undefined type of ......" .
As far as I know, forward declaration couldn't solve this problem, and
I have tried. Both of the definition of the two classes has to come
first which is not possible.
This can be solved by redesign A and B, but I wish I can find some
other solutions here, any advice will be welcome .
I'ts straightforward to solve, jus put the
implementation out of the class declaration:

----- 8< --- [class_ab.h] (declaration) ------

class A {
void f1(void);
public:
void f2(void);
};

class B {
void g1(void);
public:
void g2(void);
};

----- 8< --- [class_ab.cxx] (implementaion)---

#include "classab.h"
#include <cstdio>

void A::f1(void) {
B b;
b.g2();
printf("we are in A::f1\n");
}

void A::f2(void) {
printf("we are in A::f2\n");
}

void B::g1(void) {
A a;
a.f2();
printf("we are in B::g1\n");
}

void B::g2(void) {
printf("we are in B::g2\n");
}

----- 8< ----------------------------------
$gcc -o classab class_ab.cxx -lstdc++
Regards

Mirco
Dec 31 '07 #2
"xd*****@gmail.com" <xd*****@gmail.comwrote:
I have two classes , say A and B. A contains methods named f1() ,
f2() and B contains methods named g1(), g2().
f1() will invoke g2() and g1() will invoke f2(). This situation is
kinda like two things interact with each other.
But the program couldn't pass the compliation. g++ says " invalid use
of undefined type of ......" .
As far as I know, forward declaration couldn't solve this problem, and
I have tried. Both of the definition of the two classes has to come
first which is not possible.
This can be solved by redesign A and B, but I wish I can find some
other solutions here, any advice will be welcome .
http://www.parashift.com/c++-faq-lit....html#faq-39.1
1
Dec 31 '07 #3
Many thanks !
I found the cure, I was wrong about that forward declaration couldn't
solve this problem.
Here is my solution:
---------class_a.h----------
class B;
class A {
......
};
---------class_a.cpp----------
#include "class_a.h"
#include "class_b.h"
void A::f1() {
.........// invoke method in B
}
---------class_b.h------------
class A;
class B {
.....
};
---------class_b.cpp----------
#include "class_a.h"
#include "class_b.h"
void B::g1() {
.........// invoke method in A
}
Jan 1 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mahesh Devjibhai Dhola | last post: by
2 posts views Thread by Sidorf | last post: by
3 posts views Thread by Solution Seeker | last post: by
3 posts views Thread by musosdev | last post: by
12 posts views Thread by Joel Byrd | last post: by
34 posts views Thread by Simon Wigzell | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.