有一个 player.h 文件:
#ifndef PLAYER_H
#define PLAYER_H
/*-------------Libraries-------------*/
#include <SDL2/SDL.h>
#include <stdlib.h>
#include "map.h"
#include "view.h"
/*-------------Libraries-------------*/
/*---------Typedef---------*/
typedef struct Player
{
double x;
double y;
SDL_Texture *texture;
} Player;
/*---------Typedef---------*/
/*--------Func-Prototypes--------*/
void init_player
(
Player *player, Game_Map *game_map,
char path[], SDL_Renderer *renderer
);
/*--------Func-Prototypes--------*/
#endif
我这样编译:
#!/bin/bash
gcc -Wall -o roguelite map.c view.c player.c main.c -lSDL2
引发错误:
In file included from view.h:9,
from map.h:10,
from map.c:1:
player.h:24:21: error: unknown type name ‘Game_Map’
24 | Player *player, Game_Map *game_map,
|
由于某种原因,它在 player.h 中看不到 Game_Map 类型,尽管函数原型前有 #include "map.h"
地图.h:
...
typedef struct
{
int max_x;
int max_y;
char grid[ MAX_Y ][ MAX_X ];
SDL_Texture *floor_texture;
SDL_Texture *wall_texture;
} Game_Map;
...
...是另一个代码。
如果错误在于我如何使用#include,请使用项目链接到 github 以获取完整图片: https ://github.com/Ilya-Piskurov/SDL_Roguelite/tree/develop
解决方法很简单,因为 如果您不使用头文件中结构的任何字段,那么您根本不需要在其他头文件中包含头文件。
只需定义一个结构原型,并且只在源文件中包含头文件。
例如:
查了一下路径,原来是编译map.c文件的时候出了问题,把view.h拖到类型定义里,调用player.h就出错了。修复:在 typedef 之后放置 #include "view.h"