2012-05-15 10 views
8

Tengo el siguiente código C, que me parece muy correcto. Sin embargo, el compilador de clang (de hecho, gcc o cualquier otro compilador de C también) piensa lo contrario.No entiendo por qué el compilador me está dando el error con este código

typedef struct 
{ 
    struct timeval td_start; 
    struct timeval td_end; 
} Timer; 

void startTimer(struct Timer* ptimer) 
{ 
    gettimeofday(&(ptimer->td_start), NULL); 
} 

void stopTimer(struct Timer* ptimer) 
{ 
    gettimeofday(&(ptimer->td_end), NULL); 
} 

El compilador da los siguientes mensajes de error Waring &. ¿Alguna idea de lo que está mal aquí?

./timing.h:14:25: warning: declaration of 'struct Timer' will not be visible 
     outside of this function [-Wvisibility] 
void startTimer(struct Timer* ptimer) 
         ^
./timing.h:16:27: error: incomplete definition of type 'struct Timer' 
    gettimeofday(&(ptimer->td_start), NULL); 
        ~~~~~~^ 
./timing.h:14:25: note: forward declaration of 'struct Timer' 
void startTimer(struct Timer* ptimer) 
         ^
./timing.h:19:24: warning: declaration of 'struct Timer' will not be visible 
     outside of this function [-Wvisibility] 
void stopTimer(struct Timer* ptimer) 
        ^
./timing.h:21:27: error: incomplete definition of type 'struct Timer' 
    gettimeofday(&(ptimer->td_end), NULL); 
        ~~~~~~^ 
./timing.h:19:24: note: forward declaration of 'struct Timer' 
void stopTimer(struct Timer* ptimer) 

Respuesta

15

Retire la palabra clave struct (porque no es necesario puesto que ya typedef haya Ed la estructura):

void startTimer(Timer* ptimer) 
{ 
    ... 

void stopTimer(Timer* ptimer) 
{ 
    ... 

Alternativamente, retire la typedef:

struct Timer 
{ 
    struct timeval td_start; 
    struct timeval td_end; 
}; 

void startTimer(struct Timer* ptimer) 
{ 
    ... 

void stopTimer(struct Timer* ptimer) 
{ 
    ... 

Para obtener más información, ver Why should we typedef a struct so often in C?

+2

Una mejor solución: ¡no use el typedef! –

+0

William: ¡Pero tiene que ponerle un prefijo struct con una variable de estructura en ANSI C correcto! En C++ estoy de acuerdo con que struct no es necesario. No estoy seguro acerca de C99 sin embargo. – pythonic

4

Cualquiera que

struct Timer 
{ 
    struct timeval td_start; 
    struct timeval td_end; 
}; 

void startTimer(struct Timer* ptimer) 
{ 
    gettimeofday(&(ptimer->td_start), NULL); 
} 

O

typedef struct 
{ 
    struct timeval td_start; 
    struct timeval td_end; 
} Timer; 

void startTimer(Timer* ptimer) 
{ 
    gettimeofday(&(ptimer->td_start), NULL); 
} 

Pero no confusión.

3

ha creado un tipo llamado temporizador, basta con borrar la palabra struct antes de que los parámetros de la función, de esta manera, por ejemplo:

void startTimer(Timer* ptimer) 
{ 
    gettimeofday(&(ptimer->td_start), NULL); 
} 
2

El razón para el error es que, cuando llegue aquí

void startTimer(struct Timer* ptimer) 

no hay struct Timer en el alcance (solo un typedef para una estructura anónima). Por lo tanto, el compilador cree que desea declarar nuevo tipo struct Timer y usar un puntero como parámetro.

En realidad hacerlo sería menos que útil, ya que el tipo sería simplemente visible dentro de la función. Eso haría prácticamente imposible pasar un parámetro de fuera de la función.

Así que el compilador dice que, si bien es posible que lo permita el idioma, ¡no parece una buena idea!

Cuestiones relacionadas