Erfan Ahmadi
Erfan Ahmadi

Reputation: 31

Defning a namespace class in a header file

I have a similar problem to this but not the exact.

Assuming we have 2 header files and a main.cpp. In the first header file we have :

  namespace Logic
  {
    class GameManager;
  }

In the second header:

#include "first_header.h"
class Logic::GameManager 
{
public : 
    void init();
    void run():
};

And in the main.cpp i have :

#include "first_header.h"
int main()
{
   Logic::GameManager gm;
   gm.init();
   gm.run();
}

I get this error until i include the second header in main.cpp :

'gm' uses undefined class 'Logic::GameManager'

-Is this way of using namespaces and classes correct ?

-Is there a better way to do this?

Thanks.

Upvotes: 0

Views: 87

Answers (1)

Potatoswatter
Potatoswatter

Reputation: 137770

Re-open the namespace to define the class.

namespace Logic {

class GameManager 
{
public : 
    void init();
    void run():
};

}

And include the second header, not the first, from main.cpp. The compiler cannot find the class definition unless it is directly #include'd.

Upvotes: 1

Related Questions