C中的外部类型定义
我有4个文件: a、 h: b、 h: c、 h: d、 c: int和double只是示例,我遇到的实际问题要复杂得多。C中的外部类型定义,c,include,typedef,C,Include,Typedef,我有4个文件: a、 h: b、 h: c、 h: d、 c: int和double只是示例,我遇到的实际问题要复杂得多。 关键是,通过简单地对类型B和C进行强制转换,应该可以将类型B和C转换为A。 我所争论的问题是,它说A型被包括多次,这是可以理解的,因为d.c包括b.h,b.h包括A.h,但A.h也包括在c.h. 有什么方法可以做到这一点吗?使用include-guard,如下所示: #ifndef A_INCLUDED #define A_INCLUDED typedef struct {
关键是,通过简单地对类型B和C进行强制转换,应该可以将类型B和C转换为A。
我所争论的问题是,它说A型被包括多次,这是可以理解的,因为d.c包括b.h,b.h包括A.h,但A.h也包括在c.h.
有什么方法可以做到这一点吗?使用include-guard,如下所示:
#ifndef A_INCLUDED
#define A_INCLUDED
typedef struct {
int a;
} A;
#endif
在每个.h文件中,每个文件都有唯一的保护名称
#ifndef A_H_INCLUDED
#define A_H_INCLUDED
typedef struct {
int a;
} A;
#endif
b、 h
c、 h
d、 c
你在寻找“包括守卫”。铸造价值观?除非你喜欢切片。你可能想考虑使用包含警卫,因为你似乎没有一个循环引用,所以这应该是可解决的,没有GimiKI.可能的副本不,我已经阅读了这个问题,但不能真正弄清楚如何在我的代码中实现这一点。谢谢答案。这个ifndef、define和endif到底是做什么的?(我在谷歌上搜索了一下,但并没有找到答案……)编辑:哦,太愚蠢了-在维基百科上找到:;谢谢你的帮助!!!不客气,请接受其中一个答案来解决您的问题
#include "a.h"
typedef struct {
A a;
double c;
} C;
#include "b.h"
#include "c.h"
//Here I want to use types A, B and C
#ifndef A_INCLUDED
#define A_INCLUDED
typedef struct {
int a;
} A;
#endif
#ifndef A_H_INCLUDED
#define A_H_INCLUDED
typedef struct {
int a;
} A;
#endif
#ifndef B_H_INCLUDED
#define B_H_INCLUDED
#include "a.h"
typedef struct {
A a;
int b;
} B;
#endif
#ifndef C_H_INCLUDED
#define C_H_INCLUDED
#include "a.h"
typedef struct {
A a;
double c;
} C;
#endif
#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