Two classes and inline functions

Posted by klew on Stack Overflow See other posts from Stack Overflow or by klew
Published on 2010-02-09T22:55:22Z Indexed on 2010/03/30 21:53 UTC
Read the original article Hit count: 441

I have two classes and both of them uses some of the other class, on example:

// class1.h
class Class1;
#include "class2.h"

class Class1 {
  public:
  static Class2 *C2;
  ...
};

// class2.h
class Class2;
#include "class1.h"

class Class2 {
  public:
  static Class1 *C1;
  ...
};

And when I define it like in example above, it works (I also have some #ifndef to avoid infinite header recurency). But I also want to add some inline functions to my classes. And I read here that I should put definition of inline function in header file, because it won't work if I'll put them in cpp file and want to call them from other cpp file (when I do it I get undefined reference during linking). But the problem here is with something like this:

// class1.h
...
inline void Class1::Foo() {
  C2->Bar();
}

I get error: invalid use of incomplete type ‘struct Class2’.

So how can I do it?

© Stack Overflow or respective owner

Related posts about c++

Related posts about class