I am still new with c++ and I was told that #pragma once was supposed to take care of circular dependencies
My GameManager.h and GameSetting.h both need to know each other and each have a pointer of the other
for my GameSettings.h i have:
#pragma once
#include "PlayerManager.h"
#include <fstream>
#include "GameManager.h";
class GameSettings {
private:
PlayerManager* pM;
GameManager* gM;
vector<Player*> players;
bool inGame = false;
double betValue = 5;
bool allowSplit = true;
bool allowInsurance = true;
GameSettings(PlayerManager* pM, GameManager* gM) { this->pM = pM; this->gM = gM; }
struct GameSettingData {
vector<int> playersId;
double betValue;
bool allowSplit;
bool allowInsurance;
};
… function declarations…
for the GameSettings.cpp I only have the #include "GameSettings.h"
And for the GameManager I only have
#pragma once
#include "GameSettings.h"
class GameManager {
private:
bool isActive = false;
GameSettings *gS;
public:
bool getIsActive() { return isActive; }
bool startGame() { return true; }
};
When I remove any mention of GameSettings in GameManager everything work
I mostly know c# so this kind of circular dependencies is kind of new for me.
I did try to look at other similar questions but it did not help
>Solution :
Use a forward declaration of GameSettings in GameMager.h. Pointers do not need complete class definitions until they are derefenced.
#pragma once
class GameSettings;
class GameManager {
private:
bool isActive = false;
GameSettings *gS;
public:
bool getIsActive() { return isActive; }
bool startGame() { return true; }
};
GameManager and PlayerManager can also participate in forward declarations in GameSettings.h.
#pragma once
#include <fstream>
class PlayerManager;
class GameManager;
class GameSettings {
private:
PlayerManager* pM;
GameManager* gM;
vector<Player*> players;
bool inGame = false;
double betValue = 5;
bool allowSplit = true;
bool allowInsurance = true;
GameSettings(PlayerManager* pM, GameManager* gM) { this->pM = pM; this->gM = gM; }
struct GameSettingData {
vector<int> playersId;
double betValue;
bool allowSplit;
bool allowInsurance;
};
Set required #include in .cpp files.