Question

I am having a cyclic dependency issue. I have two header files and they each depend on each other. The issue I am having has to do with a class in a namespace.

File #1

class Player; // This is how I forward declare another class

namespace sef {

class Base
{
   public:
   Player a;
   bool SendEvent(int eventType);
};

class Sub: public Base
{
    protected:
    Player b;
    bool Execute(string a);
};
}

File #2

//class sef::Sub; // I am having trouble compiling this

class Player
{
   public:
      sef::Sub* engine; // I am having trouble compiling this
};

How do I forward declare the sef::Sub class in file #2?

Was it helpful?

Solution

Firstly, if you only declare a type, you can only use pointer or reference.

class Player; // declaration, not definition
class Base {
  Player* p;
};

Secondly, namespaces are extendable, so you can write as follow:

namespace Foo { class Player; }

And use pointer:

class Base {
  Foo::Player* p;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top