gpt4 book ai didi

c - 如何在包含文件中部分声明类型定义的结构

转载 作者:太空狗 更新时间:2023-10-29 15:01:08 24 4
gpt4 key购买 nike

作为一般做法,我正在尝试尽量减少#include 文件的相互依赖性。

在 xxx.h 中我有:

struct my_struct;  // partial decl to satisfy use of my_struct*
void funct(struct my_struct* ms); // uses the partial def

如何使用 typedef 的结构进行类似的部分声明?我在第三个 #include 中有一个实际声明,看起来像(比如在 yyy.h 中):

typedef struct my_data_s {
int ival;
... struct's other components ...
} my_data_t;

我只想在 xxx.h 中引用 typedef 的代表性声明:

typedef struct my_data_s  my_data_t;  // actual full decl is elsewhere
void funct2(my_data_t* md);

此尝试导致“typedef my_data_t 的重新定义”错误。 (使用 gcc 4.4.3/Ubuntu 10.4)其他随机搜索尝试(例如,将“{}”添加到 typedef)也会出错。

我知道编译器只需要知道该函数需要一个指针,所以看起来这应该是可能的。到目前为止,没有发现没有错误/警告的编译。

我看过其他问题和答案,找不到解决这个问题的方法。似乎应该有一个众所周知的方法来做到这一点(?!)(我知道我可以 #include yyy.y 每当我 #include xxx.h - 试图避免这种依赖性。)谢谢。

最佳答案

您是否尝试过简单的方法:?

xxx.h

struct my_data_s;
typedef struct my_data_s my_data_t;

yyy.h

#include "decl.h"
struct my_data_s {
int foo;
};

关于c - 如何在包含文件中部分声明类型定义的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9724933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com