fad*_*bee 3 c struct circular-dependency
我有两个模块,a和b.
啊:
#ifndef A_H_
#define A_H_
#include "b.h"
typedef struct {
b_t *b;
...
} a_t;
#endif // A_H_
Run Code Online (Sandbox Code Playgroud)
BH:
#ifndef B_H_
#define B_H_
#include "a.h"
typedef struct {
a_t *a;
...
} b_t;
#endif // B_H_
Run Code Online (Sandbox Code Playgroud)
如何更改它以便编译?(我想保留两个独立的编译单元.)
编辑:我忘了让结构成员指针.
使用前向声明:
啊:
struct b_t;
typedef struct a_t {
struct b_t *b;
} a_t;
Run Code Online (Sandbox Code Playgroud)
BH:
struct a_t;
typedef struct b_t {
struct a_t *a;
} b_t;
Run Code Online (Sandbox Code Playgroud)
[这个答案仅适用于原始问题,其中指针未被用作结构成员].
这不可能.自然不可能有这样的结构.
让我们说:
struct a {
struct b b;
int i;
};
struct b {
struct a a;
int i;
};
Run Code Online (Sandbox Code Playgroud)
你对此有何看法sizeof(struct a)?这个结构会爆炸,编译是不可能的.
但是,如果你让他们转向指针,它可以被编译:
struct a;
struct b {
struct a *ap;
};
struct a {
struct b *bp;
};
Run Code Online (Sandbox Code Playgroud)
这段代码确实编译:http://ideone.com/GKdUD9.