2

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.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Chris Condy
  • 626
  • 2
  • 9
  • 25
  • possible duplicate of [cyclic dependency between header files](http://stackoverflow.com/questions/2089056/cyclic-dependency-between-header-files) – Bo Persson Feb 08 '12 at 10:57

1 Answers1

6

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.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247