Solucionado (ver solução)
Solucionado
(ver solução)
3
respostas

C / Struct / Include files

Meu código só funciona importando o file mapa.c no file principal fogefoge.c. Se eu tento importar o header file ele diz que referencia das funções é undefined.

// ASSIM COMPILA
# include <stdio.h>
# include <stdlib.h>
# include "fogefoge.h"
# include "mapa.c"

MAPA m;

// ASSIM NÃO COMPILA
# include <stdio.h>
# include <stdlib.h>
# include "fogefoge.h"
# include "mapa.h"

MAPA m;
...
3 respostas

Como q ta seu arquivo mapa.h e o mapa.c? Cola o conteúdo deles pra gente ver

ARQUIVO mapa.h

struct mapa {

    char** matriz;
    int linhas;
    int colunas;

};

typedef struct mapa MAPA;

struct posicao {

    int x;
    int y;

};

typedef struct posicao POSICAO;

void libera_mapa(MAPA* m);
void aloca_mapa(MAPA* m);
void le_mapa(MAPA* m);
void acha_heroi(MAPA* m, POSICAO* p, char heroi);
void imprime_mapa(MAPA* m);

ARQUIVO mapa.c


# include <stdio.h>
# include <stdlib.h>
# include "mapa.h"

void libera_mapa(MAPA* m) {
    // Free memory
    for( int i = 0; i < m->linhas; i++ ) {
        free(m->matriz[i]);
    }
    free(m->matriz);
}

void aloca_mapa(MAPA* m) {
    // Allocate memory for matrix mapa
    m->matriz = malloc(sizeof(int*) * m->linhas);
    for( int i = 0; i < m->linhas; i++ ) {
        m->matriz[i] = malloc(sizeof(int) * (m->colunas + 1));
    }
}

void le_mapa(MAPA* m) {
    // open readable file
    FILE* f = fopen("mapa.txt", "r");
    if(f == 0) {
        printf("Erro na leitura do mapa.\n");
        exit(1);
    }

    // read first line of the file
    fscanf(f, "%d %d", &(m->linhas), &(m->colunas));
    printf("Linhas %d e colunas %d\n", m->linhas, m->colunas);

    aloca_mapa(m);

    // Read file into mapa
    for( int i = 0; i < 5; i++ ) {
        fscanf(f, "%s", m->matriz[i]);
    }

    // Close file
    fclose(f);
}

void acha_heroi(MAPA* m, POSICAO* p, char heroi) {

    // find the heroi positions on map
    for(int i = 0; i < m->linhas; i++) {
        for(int j = 0; j < m->colunas; j++) {
            if(m->matriz[i][j] == heroi) {
                p->x = i;
                p->y = j;
            }
        }
    }
}

void imprime_mapa(MAPA* m) {

    for(int i = 0; i < 5; i++) {
        printf("%s\n", m->matriz[i]);
    }
}
solução!

Geralmente o pessoa testa se o arquivo já foi incluído. Tipo mapa.h

#ifndef MAPA_H
#define MAPA_H

struct mapa {

    char** matriz;
    int linhas;
    int colunas;

};

typedef struct mapa MAPA;

struct posicao {

    int x;
    int y;

};

typedef struct posicao POSICAO;

void libera_mapa(MAPA* m);
void aloca_mapa(MAPA* m);
void le_mapa(MAPA* m);
void acha_heroi(MAPA* m, POSICAO* p, char heroi);
void imprime_mapa(MAPA* m);

#endif

Se eu não me engano não é aconselhável colocar os structs no arquivo .h, apenas fazer a referência com o typedef

Ex:

//Arquivo 'list.h':

typedef struct list * List;
// Arquivo 'list.c' 

struct list {
    unsigned length;
    char * value;
};

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software