错误C2016:C需要一个结构或联合具有结构至少有一个成员 [英] error C2016: C requires that a struct or union has at least one member from struct

查看:3818
本文介绍了错误C2016:C需要一个结构或联合具有结构至少有一个成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我这样做有2头文件:headerfile_1.h和headerfile_2.h
我得到的错误:错误C2016:C需要一个结构或联合拥有结构在headerfile_1.h的定义

When I do something like this with 2 header files: headerfile_1.h and headerfile_2.h I get error: error C2016: C requires that a struct or union has at least one member from struct A definition in headerfile_1.h

在headerfile_1.h

In headerfile_1.h

#include "headerfile_2.h"
struct a;

struct a{
    B bb;
}A;

在headerfile_2.h

In headerfile_2.h

typedef struct b{
  void (*func0)(A *aa);
}B;

请帮我明白了,我要去哪里错了。谢谢你。

Please help me understand, where am I going wrong. Thanks.

推荐答案

你正在尝试做是行不通的,因为你有两个头文件之间的循环依赖:

What you are trying to do won't work because you have circular dependencies between the two header files:

headerfile_1:

headerfile_1:

struct A{
    B bb;  /* Use of B, therefore B needs to be defined before A */
};

headerfile_2:

headerfile_2:

typedef struct b{
    void (*func0)(A a); /* Use of A, therefore A needs to be defined before B */
} B;

不可能的。

一件事你可以做的,就是要改变 func0 的定义得到一个指向 A 而不是完整的对象。这样,你真的不需要 A 定义 B

One thing you can do, is to change definition of func0 to get a pointer to A instead of the full object. This way, you don't really need the definition of A before B.

因此​​:

headerfile_1:

headerfile_1:

#include "headerfile_2.h"

struct A{
    B bb;
};

headerfile_2:

headerfile_2:

typedef struct A A;

typedef struct b{
    void (*func0)(A *a); /* A * instead of A */
} B;

这篇关于错误C2016:C需要一个结构或联合具有结构至少有一个成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆