C++ method declaration, class definition problem

Posted by John Fra. on Stack Overflow See other posts from Stack Overflow or by John Fra.
Published on 2010-12-28T17:34:34Z Indexed on 2010/12/28 17:54 UTC
Read the original article Hit count: 270

I have 2 classes: A and B. Some methods of class A need to use class B and the opposite(class B has methods that need to use class A).

So I have:

class A;

class B {

   method1(A a) {
   }

}

class A {

   method1(B b) {
   }

   void foo() {
   }
}

and everything works fine.

But when I try to call foo() of class A from B::method1 like this:

class B {

   method1(A a) {
      a.foo();
   }

}

I get as result compile errors of forward declaration and use of incomplete type. But why is this happening? (I have declared class A before using it?)

© Stack Overflow or respective owner

Related posts about c++

Related posts about declaration