Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3440116
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:23:37+00:00 2026-05-18T08:23:37+00:00

I’ve these 2 files, but the interface (and the compiler) give me this error,

  • 0

I’ve these 2 files, but the interface (and the compiler) give me this error, can you help me find what is wrong?Is really strange… should I define all body of my methods in the .cpp file?

//GameMatch.h
#pragma once
#include "Player.h"

namespace Core
{
    class GameMatch
    {
    private:
        const static unsigned int MAX_PLAYERS=20;
        unsigned int m_HumanControlled;
        Score* m_LastDeclaredScore;
        Score* m_LastScore;
        unsigned int m_MaxPlayers;
        Player* m_Players[MAX_PLAYERS];
        unsigned int m_PlayerTurn;
        inline void NextTurn() { m_PlayerTurn=(m_PlayerTurn+1U)%m_MaxPlayers; }
    public:
        GameMatch(void);
        ~GameMatch(void);
        void RemovePlayer(Player* _player);
        inline Player* getPlayingPlayer() { return m_Players[m_PlayerTurn]; }
    };
}

and

//Player.h
#pragma once
#include "IController.h"
#include "GameMatch.h"
#include <string>
#include <Windows.h>

using namespace Core::Controller;
using namespace std;

namespace Core
{
    class Player
    {
    private:
        IController* m_Controller;
        unsigned int m_Lives;
        GameMatch* m_GameMatch;
        string m_Name;
        bool m_TurnDone;
    public:
        inline void Die()
        {
            m_Lives-=1U;
            if (m_Lives<1) m_GameMatch->RemovePlayer(this);//m_GameMatch is the first error
        }
        inline const string& getName() { return m_Name; }
        inline bool IsPlayerTurn() { return (m_GameMatch->getPlayingPlayer()==this); }//m_GameMatch is the second error
        virtual void Play()=0;
        inline Player(GameMatch* _gameMatch,const char* name,unsigned int lives=3)
        {
            m_GameMatch=_gameMatch;
            m_Name=name;
            m_Lives=lives;
        }
        inline void WaitTurn() { while(!IsPlayerTurn()) Sleep(1); }
        virtual ~Player()
        {
            delete m_Controller;
        }
    };
}

But as you can see, m_GameMatch is a pointer, so I don’t understand why this error, maybe for "recursive inclusion" of header files…

UPDATE 1:

//GameMatch.h
#pragma once
#include "Score.h"
#include "Player.h"

namespace Core
{
    class GameMatch
    {
    private:
        const static unsigned int MAX_PLAYERS=20;
        unsigned int m_HumanControlled;
        Score* m_LastDeclaredScore;
        Score* m_LastScore;
        unsigned int m_MaxPlayers;
        Player* m_Players[MAX_PLAYERS];
        unsigned int m_PlayerTurn;
        inline void NextTurn();
    public:
        GameMatch(void);
        ~GameMatch(void);
        void RemovePlayer(Player* _player);
        inline Player* getPlayingPlayer();
    };
}

and

//Player.h
#pragma once
#include "IController.h"
#include "GameMatch.h"
#include <string>
#include <Windows.h>

using namespace Core::Controller;
using namespace std;

namespace Core
{
    class Player
    {
    private:
        IController* m_Controller;
        unsigned int m_Lives;
        GameMatch* m_GameMatch;
        string m_Name;
        bool m_TurnDone;
    public:
        inline void Die();
        inline const string& getName();
        inline bool IsPlayerTurn();
        virtual void Play()=0;
        inline Player(GameMatch* _gameMatch,const char* name,unsigned int lives=3);
        inline void WaitTurn();
        virtual ~Player();
    };
}

These are the 2 headers now, however it’s still not working, if I don’t include Player.h and forward declare a class in this way inside GameMatch.h:
class Player;
It works, however what if I want use some Player methods?I should re-forward declare everything… isn’t this what header files are done for? I can include an header file in a lot of places… why can’t I do it in this case?

SOLUTION:
The answer is from Alf P. Steinbach on chat:
yes, and the answer you got seems to be correct. the cyclic header dependency is a problem. there might be other problems also, but the cyclic dependency is a big one.
you don’t need a full definition of a class in order to use T*.
so the usual breaking of the cycle is to just forward-declare a class, like

class Player;

that tells the compiler that Player is a class, so you can use Player* and Player&, and even declare member functions that return Player (although you can’t define such a function until Player class is fully defined)
well as a concrete example, all that’s needed for the Core::GameMatch class definition is a forward declaration of class Player.
then in implementation file you can include "player.h".
if it’s needed for GameMatch implementation.
if you draw the files as little boxes and draw arrows to show includes, you’ll see that that gets rid of the cyclic dependency

This said, he explain that the answer I got is the correct one so I’ll mark OJ’s

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:23:38+00:00Added an answer on May 18, 2026 at 8:23 am

    You should definitely avoid the cyclic header include.

    Break the content of your functions out into .cpp files instead of putting everything in header files. Also, rather than #include things every time, just forward declare instead.

    If you remove the cyclic include, your code will most likely work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have just tried to save a simple *.rtf file with some websites and
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I have text I am displaying in SIlverlight that is coming from a CMS
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,
I have a JSP page retrieving data and when single or double quotes are
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.