2015-06-27 17 views
0

我在嘗試在c頭部使用enum時遇到了一些麻煩。下面是我的代碼看起來像在c中使用enum頭部

的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); 
} 

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); 
} 

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); 

試圖編譯這個遊戲我下面的錯誤

提前個

謝謝,任何幫助是值得歡迎

+1

移動enum聲明的頭文件/意思。現在這個聲明是在使用它之後給出的,因爲在編譯時,一個包含的文件只是將頭文件的內容複製到.c文件中 –

+0

嘗試使用'typedef'就像這樣:'typedef enum {Pequeno,Medio, Grande} Porte;' – Cyclonecode

+0

@Cyclone不,你不需要那樣做。編輯:這仍然不能解決問題。 –

回答

6

的問題是,你正在使用我們尚不知曉的編譯器,由於代碼的順序枚舉。

字面上的包含只是將頭文件的內容複製到.c文件中。所以在你的情況下,你有你的結構定義這兩個枚舉和幾行後面的枚舉定義。因此對於編譯器而言,枚舉在到達結構體時不存在。

將枚舉移動到頭文件中並在結構定義之前。

0

建議,

在頭文件中typedef的前定義兩個枚舉類型。

否則,編譯器不知道的內容在typedef結構定義

兩個枚舉聲明