簡體   English   中英

C ++兩個頭文件相互包含

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

一共有三個.h文件

啊:

#ifndef __A_H__
#define __A_H__

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

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

void getCards(A *a, int num);

#endif

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

#ifndef __CARD_H__
#define __CARD_H__

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

#endif

由於AhBh相互包含,因此未包含所有頭文件。

請給我一些建議。

據我所知,您不需要在“ Ah”文件中包含“ Bh”。 因此,將其刪除以減少依賴性。 在您的“ Bh”文件中包括“ Ah”似乎也沒有必要。 一個簡單的向前聲明就足夠了。

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