views:

166

answers:

3

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 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?

+11  A: 

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.

Paolo Tedesco
+6  A: 

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

zipcodeman
+3  A: 

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.

bromfiets
I use this flag all the time now, thank you for that tip.
zipcodeman