开发者

Macro undeclared, but defined in header

I'm facing a very weird problem.

This is map.h:

#define MAP_WIDTH   256
#define MAP_HEIGHT  256

typedef struct {
    char exit_n;
    char exit_s;
    char exit_w;
    char exit_e;
} room;

room map[MAP_WIDTH][MAP_HEIGHT];

void generate_map();

And this map.c:

#include "map.h"

void generate_map()
{
    char room_x, room_y;

    room_x = MAX_WIDTH/2;
    room_y = MAX_HEIGHT/2;

    // first开发者_StackOverflow社区 room
    map[room_x][room_y].exit_n = 1;
}

So, nothing really exotic. The problem is the compiler complaining about the two defined constants MAX_WIDTH and MAX_HEIGHT:

map.c: In function ‘generate_map’:
map.c:18: error: ‘MAX_WIDTH’ undeclared (first use in this function)
map.c:18: error: (Each undeclared identifier is reported only once
map.c:18: error: for each function it appears in.)
map.c:19: error: ‘MAX_HEIGHT’ undeclared (first use in this function)

What am I doing wrong?


It looks like you are using MAX_WIDTH (with an X) and MAP_WIDTH (with a P) in the two cases, same for the _HEIGHT constants.


In your header you say #define MAP_HEIGHT and in map.c you are trying to use MAX_HEIGHT. They are not the same.


All C compilers I know have a flag to stop after the preprocessing stage. This is pretty useful for solving preprocessor related problems. For example, gcc has the -E flag:

$ gcc -E map.c
# 1 "map.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "map.c"
# 1 "map.h" 1



typedef struct {
    char exit_n;
    char exit_s;
    char exit_w;
    char exit_e;
} room;

room map[256][256];

void generate_map();
# 2 "map.c" 2

void generate_map()
{
    char room_x, room_y;

    room_x = MAX_WIDTH/2;
    room_y = MAX_HEIGHT/2;


    map[room_x][room_y].exit_n = 1;
}

Hopefully this would have provided enough clues to spot the mistake.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜