C ++头文件交叉#include [英] C++ Header File Cross #include

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

问题描述

说,我有两个类: A && B ;一个头文件 myheader.h :

Say, I have two classes: A && B ; One Header file myheader.h :

....
class A{
public:
    void display(B* c);
};
class B{
public:
    void display(A* c);
};
.....

编译器给我错误:未声明'B'(在A :: display中)按预期.

Compiler gave me error: 'B' has not been declared( in A::display )As expected.

所以我为 A B 编写了两个单独的标题: aheader.h (包括A类的定义)和 bheader.h (包括B类);

So I wrote two separated headers for A and B: aheader.h(including definition of class A) and bheader.h(including class B);

aheader.h 中:

#include "bheader.h"

bheader.h 中:

#include "aheader.h"

到目前为止,很好.

但是当我开始编写 implementation.cpp 时会出现问题:

#include "aheader.h"
#include "bheader.h"
void A::display(B* c){}
void B::display(A* c){}

现在, A'尚未声明.(在B :: display中)

Now,A' has not been declared.(in B::display)

不知道如何简单地描述这个问题.

Don't know how to describe this problem in brief.

我正在使用Ubuntu14,Eclipse CDT,Linux GCC&Gnu Makd Builder.

I'm using Ubuntu14,Eclipse CDT,Linux GCC & Gnu Makd Builder.

我是C ++的新手,我猜链接时会发生此问题.我真的希望有人给我一个解释或解决方案.谢谢!

I'm new to C++,I guess this problem occurs when linking. And I really hope someone to give me an explanation or a solution. Thank you !

推荐答案

以C/C ++开头时,这很常见

This is quite common when beginning with C / C++

在C ++中,通常的做法是将所有不相关的类分开,每个类都放在自己的头文件中.始终(始终是这样)始终保护头文件以防止递归包含.这是通过在头文件的开头附加一个IFDEF来完成的,因此该文件仅包含一次:

The usual aproach in C++ is to have all the unrelated classes separated, each one in its own header file. Always (I mean it, ALWAYS) the header file MUST be protected against recursive inclusion. This is done by appending an IFDEF at the beginning of the header file, so the file is included only once:

I.e.提交文件fronter.h:

I. e. file aheader.h:

#ifndef __AHEADER_H__
#define __AHEADER_H__

//All the header code comes here...

#endif

因此,在包含头文件时,编译器将执行以下操作:

So when including the header file, the compiler will do the following:

  1. 是否定义了 __ AHEADER_H __ ?
  2. 尚未定义,因此会编译标头.
  3. 编译器现在定义了 __ AHEADER_H __ .
  4. 如果在其他任何时候再次包含该文件,则:
  5. 是否定义了 __ AHEADER_H __ ?
  6. 是的,不要再次编译标头.

好的,这是您问题的一部分.但是让我们来解决您的实际问题.

Ok, this is one part of your problem. But lets go to your actual problem.

出现错误是因为,正如编译器告诉您的那样,另一个类未定义.换句话说,编译器不知道A(或B)是什么.您不能在"bheader.h"中包含"aheader.h",因为这将引发"B未定义"错误:

You are having the error because, as the compiler tells you, the other class is not defined. In other words, the compiler does not know what A (or B) is. You cannot include "aheader.h" in "bheader.h", since this will throw the "B is not defined" error:

  1. 头将包括前头
  2. 提前处理开始
  3. 找到
  4. A类,其中包含带有B参数的方法.
  5. 什么是B?
  6. 这里有错误!

当然,如果以其他方式包含它们(beforeer.h中包含bheader.h),则错误将是未定义A",只是带有相同的解释.

Of course, if you include them the other way (bheader.h included in aheader.h) the error would be "A is not defined", just with the same explanation.

解决方案?

只需向前定义类:

在bheader.h中:

In bheader.h:

#ifndef __BHEADER_H__
#define __BHEADER_H__

class A; //Forward definition. We don't have any member definition, just we are telling 
         //the compiler "Trust me, this is a class"

class B{
public:
   void display(A* c);
}
#endif

必须在fronter.h中进行同样的操作.

The same would have to be done in aheader.h.

最后,为了能够在实现代码中使用A或B,您应该包括该类的实际头文件.

And finally, in order to be able to use A or B in your implementation code, you should include the actual header file of the class.

文件实现.cpp:

#include "aheader.h"
#include "bheader.h"
void A::display(B* c){}
//And so on...

只要不需要完整的类定义(即),就可以使用前向声明:

Forward declaration can be used whenever you don't need to have the complete class definition (i.e.):

  • 当类仅用作参数或函数的结果时.
  • 当类仅与指针一起使用时.

当您需要完整的类定义(即)时,就不能具有前向声明:

You cannot have forward declaration when you need to have the complete class definition (i.e.):

  • 用作班级成员.
  • 从其继承时.

顺便说一句,分离类的通常方法是将实现分成每个类一个.cpp文件,最后是:

By the way, the usual way of separating classes would be also to separate the implementation into one .cpp file per class, having at the end:

  • aheader.h
  • aheader.cpp
  • bheader.h
  • bheader.cpp

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

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