简体   繁体   English

C ++两个头文件相互包含

[英]C++ two header files include each other

There are three .h files 一共有三个.h文件

Ah: 啊:

#ifndef __A_H__
#define __A_H__

#include"Card.h"
#include"B.h"

struct A{
    Card card;
    .....
};

void getCards(A *a, int num);

#endif

Bh BH

#ifndef __B_H__
#define __B_H__

#include"Card.h"
#include"A.h"

struct B{
    Card card;
    .....
};

void getCards(A *a, B *b, int num);

#endif

Card.h Card.h

#ifndef __CARD_H__
#define __CARD_H__

struct Card{
    int num;
    char *type;
};

#endif

Since Ah and Bh includes each other, not all header files are included. 由于AhBh相互包含,因此未包含所有头文件。

Please give me some advices. 请给我一些建议。

As far as I can see, you don't need to include "Bh" in your "Ah" file. 据我所知,您不需要在“ Ah”文件中包含“ Bh”。 So remove it to reduce dependencies. 因此,将其删除以减少依赖性。 Including "Ah" in your "Bh" file also seems unnecessary. 在您的“ Bh”文件中包括“ Ah”似乎也没有必要。 A simple forward declaration should be sufficient. 一个简单的向前声明就足够了。

Bh BH

#ifndef __B_H__
#define __B_H__

#include"Card.h"

class A; // then you will have to include A.h in your B.cpp file

struct B{
    Card card;
    .....
};

void getCards(A *a, B *b, int num);

#endif

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

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