当有循环依赖时,正确的方法#include? [英] Proper way to #include when there is a circular dependency?

查看:248
本文介绍了当有循环依赖时,正确的方法#include?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用#pragma一次,不包括所有我的h文件的警卫。如果啊需要#include bh和bh需要#include啊,我该怎么办?

I'm using #pragma once, not #include guards on all my h files. What do I do if a.h needs to #include b.h and b.h needs to #include a.h?

我得到所有排序如果错误,因为这样做,pragma一次生效,其中一个失去对方。

I'm getting all sorts if errors because by doing this, the pragma once takes effect and one of them is missing each other. How should I do this.

感谢

推荐答案

向前声明你需要的定义。因此,如果A使用B作为参数值,则需要转发声明B,反之亦然。

You need to forward declare the definitions you need. So if A uses B as a parameter value, you need to forward declare B, and vice versa.

可能只是向前声明类名:

It could be that just forward declaring the class names:

 class A;
 class B;

可解决您的问题。

此问题的接受答案提供一些额外的指导。

The accepted answer to this question provides some additional guidance.

这篇关于当有循环依赖时,正确的方法#include?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆