gpt4 book ai didi

c - 在两个头文件中包含一个 typedef

转载 作者:行者123 更新时间:2023-12-02 00:23:20 26 4
gpt4 key购买 nike

我在 ball.h 中有这个:

#ifndef BALL_H_
#define BALL_H_
...
typedef void *PBALL ;
...
#endif

在 paddle.h 中我有:

#ifndef PADDLE_H_
#define PADDLE_H_
...
int contact_made(struct pppaddle*, PBALL);
...
#endif

我在 paddle.h 中遇到错误,因为它不知道 PBALL。所以如果我添加:

#ifndef BALL_H_
#include "ball.h"
#endif

到 paddle.h(有或没有 if 语句)它在我的 Cygwin 环境中工作。但是在 Linux 中,当我去编译时,我在使用 PBALL 的源文件和 ball.h 中定义的函数上得到:“multiple definition of `newPBALL'”错误。如何让 paddle.h 理解 PBALL 而不会在 Linux 中遇到这些问题?

我的 ball.c 文件:

struct newball {
int x_pos, x_dir, y_pos, y_dir, y_delay, y_count, x_delay, x_count;
char symbol;
};

typedef struct newball *ball_struct_ptr;
struct newball the_ball;

#include "ball.h"

PBALL newPBALL() {

the_ball.y_pos = Y_INIT;
the_ball.x_pos = X_INIT;
the_ball.y_count = the_ball.y_delay = Y_DELAY;
the_ball.x_count = the_ball.x_delay = X_DELAY;
the_ball.y_dir = 1;
the_ball.x_dir = 1;

the_ball.symbol = DFL_SYMBOL; //Set the symbol of the ball

PBALL ptr = &the_ball;
return ptr;
}

最佳答案

好吧,我没有尝试将一个头文件导入到另一个头文件(在 Cygwin 但在 Linux 中不起作用)或不将头文件导入另一个头文件(在 Linux 但在 Cygwin 中不起作用),我在两个头文件中都这样做了:

#ifndef TYPEDEF_PBALL_DECLARED_#define TYPEDEF_PBALL_DECLARED_typedef void *PBALL  ;#endif

现在它可以在两种环境中工作。如果有比必须在两个头文件中声明两次 typedef 更好的解决方案,我会暂时打开它。

关于c - 在两个头文件中包含一个 typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9966559/

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