简体   繁体   中英

C++ header files including each other

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM