'C unknow type "struct"
i'm having issue compiling my code. I think it has something to do with include but i don't understand i used #ifend; #endif. I think i know why but i don't understand why the ifend & endif is now working as i want to. I feel like it is because i have some kind of include recursion. But i don't get why #ifend #endif is not working.. Thanks for the help :)
Github if want to try to compile it by yourself.
game.h
#ifendef GAME_H
typedef struct Position{
int x;
int y;
// TILE_TYPE tile;
}Position;
typedef struct Room{
Position position;
int height;
int width;
// Monster **monster;
// Item **item;
}Room;
#endif
player.h
#ifndef PLAYER_H
#define PLAYER_H
#include <stdlib.h>
#include <ncurses.h>
#include "../header/game.h"
typedef struct Player{
Position position;
int health;
// Room *room;
} Player;
Player *playerSetup();
int playerMove(int y, int x, Player *user);
#endif
game.h
#ifndef GAME_H
#define GAME_H
#include "../header/player.h"
#include "../header/screen.h"
typedef struct Position{
int x;
int y;
// TILE_TYPE tile;
}Position;
typedef struct Room{
Position position;
int height;
int width;
// Monster **monster;
// Item **item;
}Room;
int handleInput(int input, Player *user);
int checkPosition(int newY, int newX, Player *unit);
#endif
Error throw by gcc
gcc -o Obj/player.o -c src/player.c -Wall
In file included from src/../header/../header/game.h:4,
from src/../header/player.h:6,
from src/player.c:1:
src/../header/../header/../header/screen.h:7:1: error: unknown type name ‘Room’
7 | Room **mapSetup();
| ^~~~
src/../header/../header/../header/screen.h:8:1: error: unknown type name ‘Room’
8 | Room *createRoom(int x, int y, int height, int width);
| ^~~~
src/../header/../header/../header/screen.h:9:14: error: unknown type name ‘Room’
9 | int drawRoom(Room *room);
| ^~~~
In file included from src/../header/player.h:6,
from src/player.c:1:
src/../header/../header/game.h:20:28: error: unknown type name ‘Player’
20 | int handleInput(int input, Player *user);
| ^~~~~~
src/../header/../header/game.h:21:39: error: unknown type name ‘Player’
21 | int checkPosition(int newY, int newX, Player *unit);
| ^~~~~~
make: *** [makefile:19: Obj/player.o] Error 1
Solution 1:[1]
You've got recursive dependencies. game.h includes player.h and player.h includes game.h. With the include guards this means that only one of them will include the other, but this also means that one will be missing something it needs.
Both game.h and player.h are dependent on Position, so move that into its own header that both game.h and player.h include.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dbush |
