[英]Forward Struct Declaration in C; not working
我阅读了所有其他尚未成功的文章(例如,在C中向前声明结构吗? )
有两个带有函数的头文件,这些函数引用彼此的头中的结构。 前向声明不起作用...肯定是因为我仍然做不正确:)有想法吗?
foo.h:
typedef struct{
...
}foostruct;
extern foostruct fooStruct; //this struct used in foo.c and other c files
typedef struct barstruct; //attempt at forward declaration
void fooFctn(barstruct *barVar); //needs definition from bar.h
bar.h:
typedef struct{
...
}barstruct;
extern barstruct barStruct; //this struct used in bar.c and other c files
typedef struct foostruct; //attempt at forward declaration
void fooFctn(foostruct *fooVar); //needs definition from foo.h
错误是
error: useless storage class specifier in empty declaration [-Werror]
src/search.h:123:18: error: unknown type name 'foostruct'
由于这些结构最初是typedef -d,所以我也只尝试了“ foostruct;”。 没有标识符(当然)不起作用,同样地,声明“ typedef struct fooVar foostruct”会产生重新定义错误。
在您的foo.h
,此代码:
typedef struct barstruct;
是一个struct barstruct
typedef
,它声明存在struct barstruct
,但没有struct barstruct
提供替代名称(因此,“空声明中的无用存储类说明符”警告/错误)。 你需要:
typedef struct barstruct barstruct;
然后代码将在foo.h
正常工作。 bar.h
需要对struct foostruct
进行相应的更改。
但是,这仍然给您带来了问题。 类型barstruct
中声明foo.h
是不相同的类型barstruct
中声明bar.h
(因为一个是带标记的结构和一个为无代码结构)。 有几种解决方法。 在C11(但不是C99或C89)中,如果它们相同,则可以重复typedef
。
因此,您将拥有两条typedef
行:
typedef struct barstruct barstruct;
typedef struct foostruct foostruct;
在标题的顶部,然后在bar.h
定义:
struct barstruct { … };
并在foo.h
:
struct foostruct { … };
如果与早期版本的C的兼容性很重要,那么您需要考虑在函数声明中使用struct barstruct
和struct foostruct
:
foo.h
typedef struct foostruct foostruct;
struct foostruct
{
…
};
extern foostruct fooStruct;
struct barstruct;
void fooFctn(struct barstruct *barVar);
和类似的bar.h
当然,这也适用于C11。
正确typedef您的结构应该可以解决您的问题。
typedef struct foostruct foostruct;
结构和标签
struct { ... };
是一个匿名结构。 它仅对typedefs有用,并且仅在该文件中使用并声明了typedef名称时才有用。 换句话说,它不能与正向声明一起使用。
但是, struct X { ... };
是struct X
类型的struct X
。 它可以用于前向声明。 请注意, X
只是用于表示特定结构类型的标签名称,而不是实际类型。 标签名称用于区分其他声明的结构类型。 有时这会使人们感到困惑,因此举个例子可能会有所帮助。 请注意,所有声明的结构类型都是不同的:
/* Declare an anonymous structure. */
struct {
int n;
};
/* Another anonymous structure. It in different from the previous one. */
struct {
double d;
};
/* Declare a structure of type `struct bar'. */
struct bar {
int n;
};
/* Declare a structure of type `struct foo'. */
struct foo {
int n;
};
/* Declare a variable `var' of type `struct foo'. */
struct foo var;
/* Unless a typedef for the `foo' type exists, or a preprocessor macro is used to replace `foo' with an actual type, this is a compilation error. */
foo var2;
请注意,如果您尝试声明两个具有相同标签名称的结构,则将导致错误。
类型定义
typedef的形式与没有初始化程序的变量声明的形式相同:
typedef TYPE NAME;
例如:
typedef int Integer;
声明Integer
类型与int
相同。 使用结构也可以这样做:
/* Typedef'd anonymous structure */
typedef struct {
int foo;
} fooBType;
/* Typedef'd `struct foostruct' structure */
typedef struct foostruct {
int foo;
} fooGType;
两者之间的区别:
fooBType
在其他文件中进行fooBType
引用的结构类型的前向声明,因为fooBType
引用的结构是匿名的 struct foostruct
通过引用fooGType
因为类型能够由结构标签和被引用是可能的struct
的关键字,如在struct foostruct
或typedef struct foostruct fooGType;
我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.