ati*_*bee 2 c struct header-files
我有一个程序分布在 3 个不同的 C 文件中,其中包含 2 个头文件。我最初有一个标题“Player.h”,我用来保存“地图”结构(等等):
#ifndef PLAYER_H_INCLUDED
#define PLAYER_H_INCLUDED
typedef struct map{
float x;
float y;
}map;
/// some other functions and structures///
#endif
Run Code Online (Sandbox Code Playgroud)
我想将它移动到它自己的头文件“Map.h”中,以便与其他与地图相关的函数和结构一起使用。
#ifndef MAP_H_INCLUDED
#define MAP_H_INCLUDED
typedef struct map{
float x;
float y;
}map;
#endif
Run Code Online (Sandbox Code Playgroud)
我照常制作了 Map.c 文件,在其中编写了一个可以工作的函数,但我还没有在 main.c 中调用该函数。我在 main.c 中制作了“map”数据结构,如下所示:
#include "Player.h"
#include "Map.h"
int main(){
... /// some other stuff being done
map map;
map.x = 0;
map.y = 0;
... /// more stuff being done
callfunctioninplayer(&map, /// other variables///)
}
Run Code Online (Sandbox Code Playgroud)
所以我在 player.c 中调用该函数,其中我还使用#included "Map.h". 但是在尝试编译它之后,它在我要求map* map带有错误的结构的任何地方都给了我一个错误:
#ifndef PLAYER_H_INCLUDED
#define PLAYER_H_INCLUDED
typedef struct map{
float x;
float y;
}map;
/// some other functions and structures///
#endif
Run Code Online (Sandbox Code Playgroud)
我检查了 map.h 是否在任何地方都定义了,确实如此,然后在将 map 结构放回 player.h 后再次对其进行了测试。把它放回 player.h 工作,但为什么它不在 map.h 中? 很抱歉,如果问这个问题很多,我浏览了一些帖子,但找不到任何有相同错误的人
看起来像是map在 Player.h 中使用,但是在包含 Player.h 时尚未包含 Map.h,并且大概您没有在 Player.h 中包含 Map.h。
如果 Player.h 需要在 Map.h 中定义,那么您需要#include "Map.h"在 Player.h 中定义。
| 归档时间: |
|
| 查看次数: |
40 次 |
| 最近记录: |