【发布时间】:2016-08-05 02:37:06
【问题描述】:
我有以下两个头文件:
player.h
#ifndef _PLAYER_H_
#define _PLAYER_H_
#include "map.h"
typedef struct{
char* name;
int x;
int y;
int keyMapping;
char symbol;
}player;
void move(map map, char* move, player player);
void setKeyMapping(int keyMap, player player);
void initPlr(char* name, int x, int y, player player);
#endif
和map.h
#ifndef _MAP_H_
#define _MAP_H_
#include "player.h"
typedef struct{
char** levelData;
int width;
int height;
}map;
void init(map* map,int height, int width);
void print_map(map map, player player);
void resume(const char *resFrom, map map);
void save(const char *saveTo, map map);
#endif
当我运行 make 文件时,我收到以下错误:
player.h:10:11: error: unknown type name ‘map’
void move(map map, int move, player player);
我是一个相对较新的程序员(在 C 中),我来到这里希望你们能帮助我弄清楚为什么头文件没有看到对方,即使我包含了它们。我使用的编译器是 gcc,我检查以确保两个头文件都包含在它们的合作伙伴 C 文件中。
我还查看了这三个帖子:
Include a header in another header file
Include headers in header file?
Should I use #include in headers?
编辑:
如果我像上面那样编辑 map.h 文件(我在 print_map 和 include 语句中添加了一个播放器参数),编译器会抛出这个错误:
player.h:12:11: error: unknown type name ‘map’
void move(map map, char* move, player player);
【问题讨论】:
-
请不要将变量命名为与其类型相同。
-
对不起,个人习惯.....我正试图杀死它,但对于程序员创建的类型,真的很难不这样做。
-
实际上很容易不这样做,通过使用诸如
map_t和player_t之类的命名约定。 -
多余的“_t”是什么意思?
-
它会告诉我
map_t是一种类型,并将其与map等实例区分开来。例如,假设你有map *map,然后你想要sizeof(map),你期望会发生什么?
标签: c gcc header-files