简体   繁体   English

C ++头文件包括彼此

[英]C++ header files including each other

I have 2 files that are both including the other file and I am getting strange errors. 我有2个文件,都包含另一个文件,并且出现了奇怪的错误。

#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 . 您在Nodemodel之间具有循环依赖关系。

To deal with this, instead of... 为了解决这个问题,而不是...

#include "Node.h"

...in model.h , forward declare... ...在model.h中,向前声明...

class Node;

...and this will allow you to have Node& node; ...这将使您拥有Node& node; in your Model class . 在您的Model class

Or vice-versa. 或相反亦然。

Better still... see if you can revisit your design and eliminate this circular dependency. 更好的是……看看您是否可以重新设计并消除这种循环依赖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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