在c中使用enum inside header

0 c enums struct header incomplete-type

我在尝试在c中使用头部枚举时遇到了一些麻烦.这是我的代码的样子

main.c中

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "listaEstatica.h"

int main(int argc, char** argv) {
    CACHORRO Tobias;
    strcpy(Tobias.nome, "Tobias");
    Tobias.registro = 123456789;
    Tobias.idade = 6;
    inserir(Tobias);

    exibirCachorro(cachorros[0]);

    return (EXIT_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)

listaEstatica.c

#include "listaEstatica.h"

fim = 0;

enum Porte { Pequeno, Medio, Grande };
enum Estado { Machucado, Doente, DoencaInfeccosa};

int vazia() {
    if (fim == 0) {
        return 1;
    }
    return 0;
}

void inserir(CACHORRO cachorro) {
    if (fim == MAX) {
        printf("Não inseriu %s, lista cheia\n", cachorro.nome);
    } else {
        cachorros[fim] = cachorro;
        fim++;
        printf("Inserido %s OK\n", cachorro.nome);
    }
}

void exibirCachorro(CACHORRO cachorro) {
    printf("Nome: %s\n", cachorro.nome);
    printf("Registro: %i\n", cachorro.registro);
    printf("Idade: %i\n", cachorro.idade);
}
Run Code Online (Sandbox Code Playgroud)

listaEstatica.h

typedef struct {
    char nome[30];
    int registro;
    int idade;
    enum Porte porte;
    enum Estado estado;
} CACHORRO;

int fim;
#define MAX 3
CACHORRO cachorros[MAX];

int vazia();

void inserir(CACHORRO cachorro);

void exibirCachorro(CACHORRO cachorro);
Run Code Online (Sandbox Code Playgroud)

试图编译这个游戏我有以下错误

 listaEstatica.h:5:16: error: field ‘porte’ has incomplete type
     enum Porte porte;
            ^
 listaEstatica.h:6:17: error: field ‘estado’ has incomplete type
     enum Estado estado;
Run Code Online (Sandbox Code Playgroud)

在此先感谢,欢迎任何帮助

rfr*_*tag 6

问题是,由于代码的顺序,您正在使用编译器尚不知道的枚举.

包含字面上只是将头文件的内容复制到.c文件中.因此,在您的情况下,您可以使用这两个枚举进行结构定义,并在枚举的下方定义几行.因此,对于编译器,枚举在到达结构时不存在.

将枚举移动到头文件中并在结构定义之前.