How to have classes call each other's method?

i have read the topic at http://www.cplusplus.com/forum/beginner/6075/
it is helpful. but i have a further question: how these two objects call each other's method? such as:

File: classA.h:
1
2
3
4
5
6
7
8
9
10
11
#ifndef CLASSA_H
#define CLASSA_H

class B;   // forward declaration

class A {
   B* pB;
   pB->B_method();
};

#endif  


File: calssB.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef CLASSB_H
#define CLASSB_H

class A;   // forward declaration

class B {
   A* pA; 
   pA->A_method();
};

#endif  


it seems can not compile becasue pB->B_method() can not be known by class A according to "class B //forward declaration". how could i achieve this without include header file?

Thanks in advance.
You can define the function bodies in an external cpp file (you wouldn't call pA->A_method out of a B member function)

eg:
1
2
3
4
5
6
7
8
//ClassB.cpp
#include "ClassA.h"
#include "ClassB.h"

void B::member_function ()
{
   pA->A_method();
}
Thanks for your solution, problem solved. definitely call pA->A_method must be in B's member function. sorry for my mistake before.
anyway, still curious if its possible just using forward declaration??
closed account (z05DSL3A)
still curious if its possible just using forward declaration??

Simple answer, No.

You can not use any members of class A until you have a definition for class A. The declaration just says that there is a class A.
Topic archived. No new replies allowed.