The macro is not declared, but defined in the header

I ran into a very strange 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 exotic. The problem is that the compiler complains about two specific 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?

+3
source share
3 answers

It looks like you are using MA X _WIDTH (with X) and MA P _WIDTH (with P) in two cases, the same for _HEIGHT constants .

+12
source

#define MAP_HEIGHT map.c MAX_HEIGHT. .

+5

All C compilers that I know have a flag to stop after the preprocessing step. This is very useful for resolving preprocessor related issues. 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 provide enough clues to detect the error.

+3
source

Source: https://habr.com/ru/post/1727684/


All Articles