【发布时间】:2021-08-04 02:33:52
【问题描述】:
我在 file.c 中定义了一个结构,并在他的头文件中为他的指针定义了一个 typedef。 我想在另一个 file.c 中使用这个 typedef,但它不起作用。我认为这是文件包含的问题。
在文件game.c中
#include <stdbool.h>
#include <stdlib.h>
#include "chessSystem.h"
#include "tournament.h"
#include "game.h"
#include "map.h"
#include "player.h"
struct Game_node
{
int game_id;
Game_data game_data;
Game next;
};
在文件game.h中我正在做:
typedef struct Game_Node *Game;
我想在另一个文件中使用这个结构:tournament.c 在一个函数中我尝试定义一个 Game 类型的变量。但我无法访问结构的字段。
#include "game.h"
..........
Game temp_game=malloc(sizeof(*temp_game));
temp_game->
重要的是,在分配 temp_game 时,IDE 会发出错误信号:“sizeof”对不完整类型“struct Game_node”的无效应用。
我知道这不是很清楚。 如果您有任何问题,请告诉我。
非常感谢。
【问题讨论】:
-
将结构体定义从C文件移动到头文件。
-
将结构定义移动到它所属的
.h文件中。如果它不在.h文件中,那么当然其他文件看不到它。想想吧。 -
请参阅Is it a good idea to typedef pointers? — TL;DR 答案通常是“否”,函数指针可能存在例外。
标签: c struct linked-list include typedef