我有4个文件:
A.H:
typedef struct {
int a;
} A;
b.h:
#include "a.h"
typedef struct {
A a;
int b;
} B;
c.h:
#include "a.h"
typedef struct {
A a;
double c;
} C;
D.C:
#include "b.h"
#include "c.h"
//Here I want to use types A, B and C
int和double只是例子,我遇到的真正问题要复杂得多
关键是应该可以通过简单地转换它来将类型B和C转换为A.
我所争论的问题是它说A类被多次包含,这是可以理解的,因为d.c包括b.h,其中包括a.h,但a.h也包含在c.h中。
有没有办法做到这一点?
答案 0 :(得分:4)
A.H
#ifndef A_H_INCLUDED
#define A_H_INCLUDED
typedef struct {
int a;
} A;
#endif
b.h
#ifndef B_H_INCLUDED
#define B_H_INCLUDED
#include "a.h"
typedef struct {
A a;
int b;
} B;
#endif
c.h
#ifndef C_H_INCLUDED
#define C_H_INCLUDED
#include "a.h"
typedef struct {
A a;
double c;
} C;
#endif
D.C
#include "a.h" // if you're going to use type A specifically do #include the proper file
#include "b.h"
#include "c.h"
//Here I want to use types A, B and C
答案 1 :(得分:3)
使用包含警戒,如下所示:
#ifndef A_INCLUDED
#define A_INCLUDED
typedef struct {
int a;
} A;
#endif
每个.h文件中的,每个文件都有唯一的防护名称