0

I'm getting the following errors that I cannot solve.

1>k:\school\c++\project_2\project_2\mechanic.h(8): fatal error C1014: too many include files : depth = 1024
1>  Maintenance.cpp
1>k:\school\c++\project_2\project_2\maintenance.h(8): fatal error C1014: too many include files : depth = 1024
(continues for many other files in the project)...

An example header file from the project: bicycle.h

//#pragma once      //Make sure it's included only once

using namespace std;
#include <iostream>

#include "Date.h"
#include "Cyclist.h"
#include "Maintenance.h"

#ifndef BICYCLE_H_
#define BICYCLE_H_

class Bicycle
{
public:
Bicycle(Date, int, int, Cyclist);
~Bicycle(void);
    Datum   getDateJoined() const;
    int getFrameSize() const;
    int getBicycleID() const;
    Cyclist getCyclist();

    void    setDateJoined(Date);
    void    setFrameSize(int);

private:
Date dateJoined;
int frameSize;
int bicycleID;
Cyclist cyclist;
list<Maintenance> maintenanceNotes;
};

#endif /* BICYCLE_H_ */
Joris Timmermans
  • 10,406
  • 2
  • 45
  • 72
Sharpless512
  • 2,641
  • 5
  • 27
  • 54

8 Answers8

9

Move your includes into the IFNDEF statement. You recursively include your files

RvdK
  • 18,577
  • 3
  • 56
  • 100
  • now i'm getting a bunch of errors like this:1>k:\school\c++\project_2\project_2\mecanicien.h(18): error C2065: 'Fiets' : undeclared identifier – Sharpless512 Aug 14 '12 at 12:18
  • @Sharpless512, Google "C++ forward declarations" – Griwes Aug 14 '12 at 12:20
  • Solving the include-guards problem does not necessarily solve all the issues of the recursive inclusion (as Sharpless512's response illustrates). I feel this answer is incomplete as it stands. – Joris Timmermans Aug 14 '12 at 12:23
  • @Sharpless512 - as I pointed out in my answer, and Griwes in the comment just above mine, you'll probably have to make use of forward declarations, though it's not the only method of solving issues like this. – Joris Timmermans Aug 14 '12 at 12:33
  • @Sharpless512: you probably have your including ordering wrong. Forward declarations can fix this but only if you use pointers/references: see http://www-subatech.in2p3.fr/~photons/subatech/soft/carnac/CPP-INC-1.shtml. Without code of these files, this question is hard to answer. – RvdK Aug 14 '12 at 12:41
4

At a guess, I would imagine you've got a circular include issue going on. Does one of your headers Datum.h, Wielrenner.h, Onderhoud.h include Fiets.h by any chance? Or do they include a file which includes it?

You can avoid this by making sure that your header - including any included headers - are guarded by pre-processor include guards e.g.

// top of file
#ifndef INCLUDED_MYFILE_H
#define INCLUDED_MYFILE_H

// ... body of the file ...

#endif
// end of file

This will prevent your file being included in a compilation unit more than once.

You may find that your build tool includes an option to show the inclusion tree which should help you find out what's happening.

Component 10
  • 9,649
  • 4
  • 42
  • 58
2

Move includes into the #ifndef FIETS_H_ to avoid including them many times.

Patrice Bernassola
  • 13,271
  • 3
  • 41
  • 54
  • so like this? 'code' #ifndef FIETS_H_ #include "Datum.h" #include "Wielrenner.h" #include "Onderhoud.h" #define FIETS_H_ 'code' – Sharpless512 Aug 14 '12 at 12:05
  • 1
    First #ifndef following by #define and then all your incldes. The goal of the guard (#ifndef) including and defining twice the same file or symbol – Patrice Bernassola Aug 14 '12 at 12:08
2

At the beginning of each header file, have an include guard:

#ifndef _THIS_HEADER_
#define _THIS_HEADER_

/* contents */

#endif

to make sure that each header is included once for each compilation unit.

perreal
  • 85,397
  • 16
  • 134
  • 168
2

The #pragma once and #ifndef / #define / #endif combination both serve the same purpose: to prevent the same definitions from being included twice into the same compilation unit accidentally. The accepted name of this construction is an "include guard".

Therefore the #pragma once should always be the very first statement in your header file*, followed immediately by the #ifndef/#define pair. Then follows the entire contents of your header file, then the #endif.

If you understand the way the preprocessor works, you can see that the very first time the header is included, the #ifndef will be true - in your example "BICYCLE_H_" will not yet be defined. Therefore the body of the #ifndef will be compiled, first #defining "BICYCLE_H_" and then declaring all the things you want to declare in the header.

The second and any consecutive time that same header file gets pulled into to the same compilation unit "BICYCLE_H_" will actually already be defined by the first time, and the #ifndef will be false. This means that the entire body of the file will be skipped, thus the end result is that almost nomatter how complicated your structure is any header will be included just once.

Now, in your particular case it's likely that if you solve this one issue you'll see another: my guess from the large depth of includes and the relatively few files is that you have an include cycle somewhere. For example, bicycle.h might include cyclist.h which in turn includes bicycle.h again, and you get an infinite, unresolvable loop. To work around this, look at "forward declaration" for example.

  • (*) #pragma once is technically compiler-specific, but widely supported. It performs the same purpose as the #ifdefs, so technically you can choose one or the other. However, you often see both at the same time "just in case" the compiler doesn't understand the #pragma once.
Community
  • 1
  • 1
Joris Timmermans
  • 10,406
  • 2
  • 45
  • 72
0

I've never hit the limit on numbers of include files, and I'm currently working on 1M LOC. My best guess would be that you have an inclusion loop: file X includes Y, Y (directly or indirectly) includes X.

Raedwald
  • 40,290
  • 35
  • 127
  • 207
0

Why did you uncomment #pragma once? It is compiler-specific and works similar to #ifndef guards. I usually use both in header files.

Also, put the guards at the very beginning, even before other #include statements.

f00id
  • 113
  • 5
0

First, you don't need #pragma once AND the #ifndef include guards. Just one is sufficient.

Second, put your includes INSIDE the include guards:

#ifndef BLAH_H
#define BLAH_H
#include <stuff>
#endif // BLAH_H

Third, why don't you forward declare needed classes in headers, then include them in sources?

#ifndef BICYCLE_H_
#define BICYCLE_H_

class Date;
class Cyclist;
class Maintenance;

class Bicycle
{
public:
Bicycle(Date, int, int, Cyclist);
~Bicycle(void);
    Datum   getDateJoined() const;
    int getFrameSize() const;
    int getBicycleID() const;
    Cyclist getCyclist();

    void    setDateJoined(Date);
    void    setFrameSize(int);

private:
Date dateJoined;
int frameSize;
int bicycleID;
Cyclist cyclist;
list<Maintenance> maintenanceNotes;
};

#endif /* BICYCLE_H_ */
derpface
  • 1,473
  • 9
  • 19
  • For the third, do you need to change anything else or just replace the date.h with "class Date;" ? – Sharpless512 Aug 14 '12 at 13:55
  • If you intend to store a Date by value (composition), then you will need to #include it in the header. If the relationship is aggregation or association (you have pointers to it), or if it's just a dependency (you take it as a function parameter), then the forward declaration is all you need. Basically if your class declaration doesn't require knowledge of its size or internals (which is any relationship below composition), then you can just use a forward declaration. – derpface Aug 14 '12 at 16:40