Bonjour,

J'ai un fichier main.c :


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <stdbool.h>
#include "map.h"
#include "hero.h"
 
int main()
{
    int map[9][9] = {
        {0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0}};
 
    Hero hero;
    hero.potion = 5;
    hero.pv = 1;
 
    aff_map(map, &hero);
 
    return 0;
}
_________________________________
Puis j'ai un hero.h et hero.c :

hero.h :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
#ifndef HERO_H
#define HERO_H
 
typedef struct Hero
{
    int potion;
    int pv;
} Hero;
 
void aff_pv(Hero *hero);
#endif
hero.c :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
#include "hero.h"
#include <stdio.h>
 
void aff_pv(Hero *hero)
{
    printf("pv:%d\n", hero->pv);
    printf("potion:%d\n", hero->potion);
}
__________________________________

Et enfin un map.h et map.c :

map.h :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
#ifndef MAP_H
#define MAP_H
 
void aff_map(int map[9][9], Hero *hero);
 
#endif
map.c :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "map.h"
#include "hero.h"
#include <stdio.h>

void aff_map(int map[9][9], Hero *hero)
{

    aff_pv(&hero);

    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            printf("%d", map[i][j]);
        }
        printf("\n");
    }
}
J'ai souligné dans le fichier map.c la fonction que je cherche a utiliser dans la fonction aff_map().

Je n'arrive pas a faire ceci avec une structure, pourtant si je cherche a faire la même chose pour la fonction aff_map() dans le fichier hero.c, il me suffit de lui inclure le fichier map.h et de mettre ma fonction dans la fonction aff_pv() du fichier hero.c et ça fonctionne !

Voici le message d'erreur en console lors de la compilation avec gcc :

PS C:\Users\shamb\MonProg> gcc -Wall main.c map.c -o monCode.exe
In file included from main.c:4:
map.h:4:29: error: unknown type name 'Hero'
    4 | void aff_map(int map[9][9], Hero *hero);
      |                             ^~~~
main.c: In function 'main':
main.c:24:5: warning: implicit declaration of function 'aff_map' [-Wimplicit-function-declaration]
   24 |     aff_map(map, &hero);
      |     ^~~~~~~
In file included from map.c:1:
map.h:4:29: error: unknown type name 'Hero'
    4 | void aff_map(int map[9][9], Hero *hero);
      |                             ^~~~
map.c: In function 'aff_map':
map.c:8:12: warning: passing argument 1 of 'aff_pv' from incompatible pointer type [-Wincompatible-pointer-types]
    8 |     aff_pv(&hero);
      |            ^~~~~
      |            |
      |            Hero **
In file included from map.c:2:
hero.h:10:19: note: expected 'Hero *' but argument is of type 'Hero **'
   10 | void aff_pv(Hero *hero);
      |             ~~~~~~^~~~
Quelqu'un a t il une solution pour utiliser une fonction comportant une structure d'un fichier externe, dans une autre fonction d'un autre fichier externe ?

Remarque : même sans passer par une fonction, le fait de vouloir utiliser une structure ailleurs que dans le main.c donne aussi des erreurs lors de la compilation.

Merci par avance.