C++ header files including each other

cheader

I have 2 files that are both including the other file and I am getting strange errors.

#ifndef NODE_H
#define NODE_H

#include "model.h"
etc....
#endif

#ifndef MODEL_H
#define MODEL_H

#include "Node.h"
etc....
#endif

Here is my example code of what I am doing. Can somebody explain to me why this is not possible or allowed? And what I should do to get passed this problem.

Best Answer

You have a circular dependency between Node and model.

To deal with this, instead of...

#include "Node.h"

...in model.h , forward declare...

class Node;

...and this will allow you to have Node& node; in your Model class.

Or vice-versa.

Better still... see if you can revisit your design and eliminate this circular dependency.