0
//main.cpp
#include <iostream>
#include <array>
#include <iomanip>
#include "Board.h"
#include "Game.h"
//Player.h
#include <array>
#include <string.h>
#include <random>
#include "Property.h"
#include "Game.h"
#ifndef Player_h
#define Player_h
//Property.h
#include "Space.h" //#include nested too deeply error
#ifndef Property_h
#define Property_h
//Space.h
#include <sstream>
#include <string>
#ifndef Space_h
#define Space_h
//FreeParking.h + a couple others inheriting from space; there's no error in any of these either
#include "Space.h"
#ifndef FreeParking_h
#define FreeParking_h
//Board.h
#include "Player.h"
#include <array>
#include <random>
#ifndef Board_h
#define Board_h
//Game.h
#include <array>
#include "Property.h"
#include "CommunityChest.h"
#include "Tax.h"
#include "FreeParking.h"
#include "Jail.h"
#include "GoToJail.h"
#include "Go.h"
#include "Player.h"
#ifndef Game_h
#define Game_h

I don't think I made any changes to the #includes today but just got this error even though the program was running fine 20 minutes ago. I only posted the includes because I'm not sure if the actual code matters for this error or not. If it does I'll try to go over the stuff I wrote today, but most of it was just changing things that already worked previously.

John
  • 33
  • 2
  • 3
    The *point* of the `#ifndef` you've written is to protect against cyclic includes. It can't do that if the includes are *outside* the guards. – Silvio Mayolo Jan 21 '22 at 00:52
  • 2
    Hint: a header file should only include the headers needed to resolve the symbols *in the header file*. Let the source file include other files (although same rule applies). For example, if the header file doesn't refer to anything in `cstdint`, don't include `cstdint`. – Thomas Matthews Jan 21 '22 at 00:58
  • 1
    Recommendation: Keep backups. If the program suddenly stops working, compare what you have against the back-up. Odds are the mistake will stand out in the differences more easily than it does in the program's full code. – user4581301 Jan 21 '22 at 01:28

1 Answers1

1

Player.h includes Game.h and Game.h includes Player.h. This is an infinite loop. There might be more, but that's just the first one I saw.

You should remove at least one of those includes to break the infinite loop. If you get errors when you do that, you might be able to fix them using a forward declaration that looks something like this:

class Player;

A forward declaration like that would allow you to compile some code that uses the Player class, even though a complete definition of the Player class is not available at that point in the program.

Two more tips to make things more sane:

  • Put your include guards at the very top of your file before you include anything.
  • Use #pragma once as the include guard instead of your more complicated thing.
David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • Removing game.h from player.h did allow me to run the program but the error still remains. Where should I be putting #pragma once if I'm going to use that? Should every header file have it or only in like space.h or property.h since that's where the errors are occurring? – John Jan 21 '22 at 01:07
  • I got rid of the error by putting the #include space under the #ifndef and #define in the property class, but would that also fix the error if I changed whichever headers needed #pragma once? – John Jan 21 '22 at 01:13
  • 1
    I think every header should have `#pragma once` at the top of it. I'm not sure what happens if you put it somewhere else in the file and that doesn't seem very useful. I'm not sure what you're asking for your second question, but you might as well try it and see. – David Grayson Jan 21 '22 at 01:31