Problem : cross reference between classes

X

xdli888

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 .
 
M

Mirco Wahab

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
 
D

Daniel T.

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-lite/misc-technical-issues.html#faq-39.1
1
 
X

xdli888

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
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,536
Latest member
VeldaYoung

Latest Threads

Top