C++ – Nested include – Avoiding ‘include nested too deeply error’

As others have suggested, use header guards. But also try forward declaring the classes in question. You may also have to work with pointers (rather than values) in at least one of your classes, but without seeing the code, we can’t tell.

So edge.h should like something like:

#ifndef EDGE_H
#define EDGE_H

class Node;    // forward declaration

Node functionB();

#endif

Note that you will have to define your function in a separate C++ file which then #includes “node.h”.

If all this seems very complicated, then you should try simplifying your design. It is probably not necessary for nodes and edges to know about each other — a one way dependency should suffice.

And lastly, names containing double-underscores are reserved in C++ — you are not allowed to create such names in your own code.

Leave a Comment