2009-09-29 13 views
15

Estoy intentando convertir una estructura de C++ en C, pero sigo obteniendo el "identificador no declarado"? ¿Tiene C++ una sintaxis diferente para referirse a las estructuras?Diferencias entre struct en C y C++

struct KEY_STATE 
{ 
    bool kSHIFT; //if the shift key is pressed 
    bool kCAPSLOCK; //if the caps lock key is pressed down 
    bool kCTRL; //if the control key is pressed down 
    bool kALT; //if the alt key is pressed down 
}; 

estoy usando una variable de tipo KEY_STATE dentro de otra estructura:

typedef struct _DEVICE_EXTENSION 
{ 
    WDFDEVICE WdfDevice; 
    KEY_STATE kState; 
} DEVICE_EXTENSION, *PDEVICE_EXTENSION; 

resultados en error C2061: error de sintaxis: identificador 'KEY_STATE'

... en la línea KEY_STATE kState; Estoy compilando con el compilador WDK si eso hace alguna diferencia. Esto está en un archivo de encabezado, por supuesto. Estoy portando el controlador C++ WDM a WDF y C.

This is the MSDN article for C2061.

Un inicializador puede ir entre paréntesis. Para evitar este problema, encierre el declarador entre paréntesis o conviértalo en un typedef.

Este error también puede deberse a que el compilador detecta una expresión como un argumento de plantilla de clase; use typename para decirle al compilador que es un tipo.

Cambiar KEY_STATE a typedef struct todavía causa este error y en realidad causa mucho más. No hay paréntesis gratuitos o cosas en demasiados paréntesis, esa es la otra cosa que sugiere el artículo.

Respuesta

30

En C, el nombre del tipo es struct KEY_STATE.

lo que tiene que declarar la segunda estructura como

typedef struct _DEVICE_EXTENSION 
{ 
    WDFDEVICE WdfDevice; 
    struct KEY_STATE kState; 
} DEVICE_EXTENSION, *PDEVICE_EXTENSION; 

Si no desea escribir struct todo el tiempo, puede utilizar un typedef declarar KEY_STATE similar a DEVICE_EXTENSION:

typedef struct _KEY_STATE 
{ 
    /* ... */ 
} KEY_STATE; 
+1

gracias Timbo, solucionó mi problema :) –

6

Tiene que referirse a KEY_STATE con struct KEY_STATE. En C++ se puede dejar el struct, pero no en la llanura C.

Otra solución es hacer un tipo de alias:

typedef struct KEY_STATE KEY_STATE

Ahora KEY_STATE significa lo mismo que struct KEY_STATE

+0

Uhm, ¿Alguien acaba de downvote todas las respuestas? ¿Por qué? – hrnt

+0

+1 por ser claro, corto y preciso. –

5

Puede/debe escribir la estructura para que no necesite la palabra clave struct cada vez que declare una variable de ese tipo.

typedef struct _KEY_STATE 
{ 
    bool kSHIFT; //if the shift key is pressed 
    bool kCAPSLOCK; //if the caps lock key is pressed down 
    bool kCTRL; //if the control key is pressed down 
    bool kALT; //if the alt key is pressed down 
} KEY_STATE; 

Ahora se puede hacer:

KEY_STATE kState; 

o (como en el ejemplo que tiene):

struct KEY_STATE kState; 
4

usted tiene que calificar una variable struct con la palabra clave 'struct':

typedef struct _DEVICE_EXTENSION 
{ 
    WDFDEVICE WdfDevice; 
    struct KEY_STATE kState; 
} DEVICE_EXTENSION, *PDEVICE_EXTENSION; 

Cuando utiliza DEVICE_EXTENSION usted d on't tiene que hacer uso de 'struct' porque está haciendo una definición de estructura y un typedef en una sola instrucción compuesta. Por lo que podría hacer lo mismo para KEY_STATE si desea utilizarlo en un fasion similar:

typedef struct _KEY_STATE_t 
{ 
    bool kSHIFT; //if the shift key is pressed 
    bool kCAPSLOCK; //if the caps lock key is pressed down 
    bool kCTRL; //if the control key is pressed down 
    bool kALT; //if the alt key is pressed down 
} KEY_STATE; 
16

No hay bool tipo en C antes de su C99.

Además, no hay ningún tipo llamado KEY_STATE cuando haces struct KEY_STATE.

probar este lugar:

typedef struct _KEY_STATE 
{ 
    unsigned kSHIFT : 1; //if the shift key is pressed 
    unsigned kCAPSLOCK : 1; //if the caps lock key is pressed down 
    unsigned kCTRL : 1; //if the control key is pressed down 
    unsigned kALT : 1; //if the alt key is pressed down 
} KEY_STATE; 
+0

+1 gracias, el bool estaba causando un montón más errores que realmente no entendía –