Pregunta

Estoy tratando de convertir una estructura de C ++ a C, pero seguir recibiendo "identificador no declarado"? C ++ no tiene una sintaxis diferente para referirse a estructuras?

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 utilizando 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 construyendo con el compilador WDK si hay alguna diferencia. Esto está en un archivo de cabecera por supuesto. Estoy portar conductor C ++ WDM a WDF y C.

Este es el artículo de MSDN para C2061 .

  

Un inicializador puede estar encerrada entre paréntesis. Para evitar este problema, incluya el declarador entre paréntesis o que sea un typedef.

     

Este error también podría ser causada cuando el compilador detecta una expresión como un argumento de plantilla de clase; nombretipo utilizar para indicar al compilador que es un tipo.

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

¿Fue útil?

Solución

En C, el nombre del tipo es struct KEY_STATE.

Así que hay 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, se puede utilizar un typedef KEY_STATE declarar similar a DEVICE_EXTENSION:

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

Otros consejos

No hay ningún tipo bool en C antes de C99.

Además, no existe un tipo llamado KEY_STATE cuando haces struct KEY_STATE.

Tal vez puedas probar:

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;

Es necesario hacer referencia a KEY_STATE con struct KEY_STATE. En C ++ se puede dejar el struct, pero no en C plano.

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

Se puede / debe typedef struct el fin de que no se necesita la palabra clave struct cada vez que va a declarar 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;

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

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

Cuando se utiliza DEVICE_EXTENSION usted no tiene que hacer uso 'struct' porque están haciendo una definición struct y un typedef en una sentencia compuesta única. 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;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top