Les différences entre struct en C et C ++
Question
Je suis en train de convertir un struct C ++ à C, mais continuer à obtenir « identificateur non déclaré »? Est-ce que C ++ ont une syntaxe différente pour faire référence à structs?
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
};
J'utilise une variable de type KEY_STATE dans une autre structure:
typedef struct _DEVICE_EXTENSION
{
WDFDEVICE WdfDevice;
KEY_STATE kState;
} DEVICE_EXTENSION, *PDEVICE_EXTENSION;
résultats dans C2061 erreur: erreur de syntaxe: identificateur 'KEY_STATE'
... sur la ligne KEY_STATE kState; Je construis avec le compilateur WDK si cela fait une différence. Ceci est dans un fichier d'en-tête bien sûr. Je PORTAGE C ++ pilote WDM à WDF et C.
Voici l'article MSDN pour C2061 .
Un initialiseur peut être entouré par des parenthèses. Pour éviter ce problème, placez-le déclarateur entre parenthèses ou faire un typedef.
Cette erreur peut également être provoquée lorsque le compilateur détecte une expression comme argument de modèle de classe; utiliser typename pour indiquer au compilateur est un type.
Modification KEY_STATE à struct typedef provoque toujours cette erreur et provoque en fait beaucoup plus. Il n'y a pas entre parenthèses libres ou des choses dans trop de parenthèses, qui est l'autre chose que l'article suggère.
La solution
En C, le nom du type est struct KEY_STATE
.
Vous devez déclarer la deuxième struct comme
typedef struct _DEVICE_EXTENSION
{
WDFDEVICE WdfDevice;
struct KEY_STATE kState;
} DEVICE_EXTENSION, *PDEVICE_EXTENSION;
Si vous ne voulez pas écrire struct
tout le temps, vous pouvez utiliser un typedef déclarer KEY_STATE
semblable à DEVICE_EXTENSION
:
typedef struct _KEY_STATE
{
/* ... */
} KEY_STATE;
Autres conseils
Il n'y a pas de type bool
en C avant C99.
En outre, il n'y a pas de type appelé KEY_STATE
quand vous faites struct KEY_STATE
.
Essayez ceci:
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;
Vous devez consulter KEY_STATE
avec struct KEY_STATE
. En C ++, vous pouvez quitter le struct
, mais pas en C ordinaire.
Une autre solution consiste à faire un alias de type:
typedef struct KEY_STATE KEY_STATE
KEY_STATE
signifie la même chose que struct KEY_STATE
Vous pouvez / devez typedef struct afin que vous n'avez pas besoin du mot-clé struct chaque fois que vous déclarez une variable de ce type.
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;
Maintenant, vous pouvez faire:
KEY_STATE kState;
ou (comme dans l'exemple que vous avez):
struct KEY_STATE kState;
Vous devez vous qualifier une variable struct avec le mot-clé 'struct':
typedef struct _DEVICE_EXTENSION
{
WDFDEVICE WdfDevice;
struct KEY_STATE kState;
} DEVICE_EXTENSION, *PDEVICE_EXTENSION;
Lorsque vous utilisez DEVICE_EXTENSION vous ne devez pas faire usage « struct » parce que vous faites une définition struct et un typedef en une seule instruction composée. Ainsi, vous pouvez faire la même chose pour KEY_STATE si vous vouliez l'utiliser dans un fasion similaire:
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;