如何在 .h 中转发 typedef 结构

发布于 2024-12-02 02:36:33 字数 640 浏览 1 评论 0原文

我有 Preprocessor.h

#define MAX_FILES 15

struct Preprocessor {
    FILE fileVector[MAX_FILES];
    int currentFile;
};

typedef struct Preprocessor Prepro;

void Prepro_init(Prepro* p) {
    (*p).currentFile = 0;
}

然后我意识到我必须将声明与定义分开。所以我创建了 Preprocessor.c:

#define MAX_FILES 15

struct Preprocessor {
    FILE fileVector[MAX_FILES];
    int currentFile;
};

typedef struct Preprocessor Prepro;

现在 Preprocessor.h 是:

void Prepro_init(Prepro* p) {
    (*p).currentFile = 0;
}

显然,这不起作用,因为 Pr..h 不知道 Prepro 类型。我已经尝试了几种组合,但没有一个有效。我找不到解决方案。

I have
Preprocessor.h

#define MAX_FILES 15

struct Preprocessor {
    FILE fileVector[MAX_FILES];
    int currentFile;
};

typedef struct Preprocessor Prepro;

void Prepro_init(Prepro* p) {
    (*p).currentFile = 0;
}

I realized then that I had to separate declarations from definitions. So I created Preprocessor.c:

#define MAX_FILES 15

struct Preprocessor {
    FILE fileVector[MAX_FILES];
    int currentFile;
};

typedef struct Preprocessor Prepro;

And Preprocessor.h is now:

void Prepro_init(Prepro* p) {
    (*p).currentFile = 0;
}

That obviously, doesn't work because Pr..h doesn't know Prepro type. I already tried several combinations, none of them worked. I can't find the solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

春风十里 2024-12-09 02:36:33

typedef struct Preprocessor Prepro; 移至该文件的标头以及 c 文件中的定义以及 Prepro_init 定义。这将毫无问题地为您转发声明。

预处理器.h

#ifndef _PREPROCESSOR_H_
#define _PREPROCESSOR_H_

#define MAX_FILES 15

typedef struct Preprocessor Prepro;

void Prepro_init(Prepro* p);

#endif

预处理器.c

#include "Preprocessor.h"

#include <stdio.h>

struct Preprocessor {
    FILE fileVector[MAX_FILES];
    int currentFile;
};

void Prepro_init(Prepro* p) {
    (*p).currentFile = 0;
}

Move the typedef struct Preprocessor Prepro; to the header the file and the definition in the c file along with the Prepro_init definition. This is will forward declare it for you with no issues.

Preprocessor.h

#ifndef _PREPROCESSOR_H_
#define _PREPROCESSOR_H_

#define MAX_FILES 15

typedef struct Preprocessor Prepro;

void Prepro_init(Prepro* p);

#endif

Preprocessor.c

#include "Preprocessor.h"

#include <stdio.h>

struct Preprocessor {
    FILE fileVector[MAX_FILES];
    int currentFile;
};

void Prepro_init(Prepro* p) {
    (*p).currentFile = 0;
}
绮烟 2024-12-09 02:36:33

如果您想隐藏 Preprocessor 的定义,只需将其放入头文件中即可:

struct Preprocessor;
typedef struct Preprocessor Prepro;

但更一般而言,您可能还需要标头中的 Preprocessor 定义文件,以允许其他代码实际使用它。

If you want to hide the definition of Preprocessor, you can simply put this in the header file :

struct Preprocessor;
typedef struct Preprocessor Prepro;

But more generally, you'll probably also need the Preprocessor definition in the header file, to allow other code to actually use it.

厌倦 2024-12-09 02:36:33

您已将应在 .h 中的内容放入 .c 中,反之亦然。 Prepro_init 必须位于 .c 文件中,并且该文件必须#include "Preprocessor.h"

You have put in .c what should be in .h, and vice versa. Prepro_init must be in .c file, and that file must #include "Preprocessor.h".

一身骄傲 2024-12-09 02:36:33

YAS:另一个解决方案。

Preprocessor.h

<some code>
    void Prepro_init(Prepro* p) {
        (*p).currentFile = 0;
    }
<some code>

Preprocessor.c

#define MAX_FILES 15

struct Preprocessor {
    FILE fileVector[MAX_FILES];
    int currentFile;
};
typedef struct Preprocessor Prepro;

#include "Preprocessor.h"      //include after defining your structure.

         <some code> 
        {
              struct Prepro p;
              Prepro_init(p);
     <some code> 
          .... using p.currentFile.....
          .....using other members....
     <some code> 
        }
           <some code> 

现在它可以工作了。我想这就是你的要求。希望有帮助。

缺点:
Preprocessor 结构的成员必须是预先确定的。即头文件使用成员currentFile。因此,包含 Preprocessor.h 的 c 文件必须具有类型定义为 Prepro 的结构,并且该结构必须包含成员 currentFile。(在本例中)。

一年前,我在编写头文件以图形树格式显示用户 Avl 树时遇到了同样的问题。

YAS:Yet Another Solution.

Preprocessor.h

<some code>
    void Prepro_init(Prepro* p) {
        (*p).currentFile = 0;
    }
<some code>

Preprocessor.c

#define MAX_FILES 15

struct Preprocessor {
    FILE fileVector[MAX_FILES];
    int currentFile;
};
typedef struct Preprocessor Prepro;

#include "Preprocessor.h"      //include after defining your structure.

         <some code> 
        {
              struct Prepro p;
              Prepro_init(p);
     <some code> 
          .... using p.currentFile.....
          .....using other members....
     <some code> 
        }
           <some code> 

Now it will work. I think this is your requirement. Hope it helps.

Drawback:
The members of the structure Preprocessor, must be predetermined. i.e the header file uses the member currentFile. So, c file which includes Preprocessor.h must have a structure which is typedefined as Prepro and that structure must include a member currentFile.(in this case).

The same problem I had a year before, while writting a header file to display a users Avl tree in a graphical tree format.

行至春深 2024-12-09 02:36:33

我建议您遵循 Linus[1],并且不要使用 typedef struct。

如果您可以控制库:

包含文件

struct Foo;

int foo_get_n(const struct Foo *bar);

在实现文件中

struct Foo
{int n;};

int foo_get_n(const struct Foo *bar)
{return bar->n;}

如果您无法控制库:

要求维护者从包含文件中删除这些污染性的 typedef。

摘要

不要使用 typedef struct。

[1] http://yarchive.net/comp/linux/typedefs.html

I would suggest that you follow Linus[1], and do not use typedef struct.

If you are in control over the library:

Include file

struct Foo;

int foo_get_n(const struct Foo *bar);

In implementation file

struct Foo
{int n;};

int foo_get_n(const struct Foo *bar)
{return bar->n;}

If you are not in control over the library:

Ask the maintainer to remove these polluting typedefs from the include files.

Summary

Do not use typedef struct.

[1] http://yarchive.net/comp/linux/typedefs.html

如果没有 2024-12-09 02:36:33

交换 .h.c 文件。在 .c 中包含标头。

另请参阅有关声明、定义和头文件用途的书籍。

Swap .h and .c file. Include header in .c.

Also refer to a book about declarations, definitions and what header files do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文