C ++循环头依赖 [英] C++ cyclical header dependency

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

问题描述

我有一个问题:假设我有一个标题/源文件集和一个标题设置如下

I had a question: Supposed I have a header/source file set and a header set as follows"

BaseCharacter.h和BaseCharacter.cpp和EventTypes.h

BaseCharacter.h and BaseCharacter.cpp and EventTypes.h

BaseCharacter.h使用EventTypes.h中定义的struct和typedef,但EventTypes.h必须知道BaseCharacter.h中定义的BaseCharacter类,如果我取出EventTypes.h和所有依赖于EventTypes.h中的东西的方法,我的程序编译得很好,但是如果我添加了EventTypes.h ,它和引用BaseCharacter.h的每个文件都会抱怨它找不到BaseCharacter类。

BaseCharacter.h makes use of structs and typedefs defined in EventTypes.h, but EventTypes.h has to be aware of the BaseCharacter class defined in BaseCharacter.h. This creates a cyclical dependency and I'm pretty sure that's whats stopping my program from compiling. If I take out EventTypes.h and all the methods that rely on the stuff in EventTypes.h, my program compiles fine. But if I added EventTypes.h, it, and every file referencing BaseCharacter.h will complain that it can't find the BaseCharacter class.

有一个绕过这个依赖的方法,导致我的问题?

Is there a way around this dependency or would this not be what's causing my problem?

我使用MSVC 2010作为我的编译器

I'm using MSVC 2010 as my compiler

推荐答案

转发声明。

在EventTypes.h中,删除include并添加:

In EventTypes.h, remove the include and add:

class BaseCharacter; c>

请注意,你只能使用引用和指针 BaseCharacter 在EventTypes.h中,你不能eg有一个 BaseCharacter myChar; 成员变量的结构。

Note that you can only use references and pointers to BaseCharacter within EventTypes.h, you cannot e.g. have a struct with a BaseCharacter myChar; member variable.

这篇关于C ++循环头依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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