Search code examples
cstructheader-files

Cant send pointer to structure after it moved to a different header file


I have a program spread through 3 different C files, with the 2 header files that come with it. I originally had the header "Player.h" that I used to save the "map" structure (among others):

#ifndef PLAYER_H_INCLUDED
#define PLAYER_H_INCLUDED
typedef struct map{
    float x;
    float y;
}map;
/// some other functions and structures///
#endif

I wanted to move it to its own header file, "Map.h" so its with the other map-related functions and structures.

#ifndef MAP_H_INCLUDED
#define MAP_H_INCLUDED
typedef struct map{
    float x;
    float y;
}map;
#endif

I made the Map.c file as normal, wrote a function in it that would work, but I didn"t call for the function in main.c yet. I made the "map" data structure in main.c as followed:

#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///)
}

so I call the function in player.c, where I also included map.h using #included "Map.h". But after trying to compile it, it gave me an error everywhere where I asked for the map* map structure with the errors:


In file included from ./main.c:10:0:
./Player.h:21:55: error: unknown type name ‘map’
 void movementPlayer(int* inputPlayer, player* player, map* map, unsigned int countFrames);

./Player.h:21:55: error: unknown type name ‘map’
 void movementPlayer(int* inputPlayer, player* player, map* map, unsigned int countFrames);
                                                       ^~~
In file included from ./Map.c:10:0:
./Player.h:21:55: error: unknown type name ‘map’
 void movementPlayer(int* inputPlayer, player* player, map* map, unsigned int countFrames);

I checked if map.h was defined everywhere, it is, and I tested it again after putting the map struct back in player.h. Putting it back in player.h worked, but why didn't it in map.h? I'm sorry if this is asked a lot, I looked through some posts but couldn't find anyone with the same error


Solution

  • It looks like map is being used in Player.h, but Map.h hasn't been included at the point Player.h is included, and presumably you didn't include Map.h in Player.h.

    If Player.h needs definitions in Map.h, then you need to #include "Map.h" in Player.h.