How to resolve circular dependency with friend declarations in C++? -


why doesn't following code compile , how can fix it? error is:

use of undeclared identifier 'foo'

although foo declared , defined @ point error occurs (at friend declaration in bar).

foo.h:

#ifndef foo_h #define foo_h  #include "bar.h" // needed friend declaration in foochild  class foo { public:   void func(const bar&) const; };  class foochild : public foo {   friend void bar::func(foochild*); };  #endif 

foo.cpp:

#include "foo.h"  void foo::func(const bar& b) const {   // stuff b.x } 

bar.h:

#ifndef bar_h #define bar_h  #include "foo.h"  class bar { public:   void func(foochild*) {} private:   int x;   friend void foo::func(const bar&) const; // "use of undeclared identifier 'foo'" };  #endif 

here's compilable online version of above code.

put foochild in header file of own.

foo.h:

#ifndef foo_h #define foo_h  class bar;  class foo { public:   void func(const bar&) const; };  #endif 

foochild.h:

#ifndef foochild_h #define foochild_h  #include "foo.h" #include "bar.h"  class foochild : public foo {   friend void bar::func(foochild*); };  #endif 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -