I am trying to convert a C++ struct to C but keep getting "undeclared identifier"? Does C++ have a different syntax for referring to 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
};
I am using a variable of type KEY_STATE inside another structure:
typedef struct _DEVICE_EXTENSION
{
WDFDEVICE WdfDevice;
KEY_STATE kState;
} DEVICE_EXTENSION, *PDEVICE_EXTENSION;
results in error C2061: syntax error : identifier 'KEY_STATE'
...on the line KEY_STATE kState; I am building with the WDK compiler if that makes any difference. This is in a header file of course. I am porting C++ WDM driver to WDF and C.
This is the MSDN article for C2061.
An initializer may be enclosed by parentheses. To avoid this problem, enclose the declarator in parentheses or make it a typedef.
This error could also be caused when the compiler detects an expression as a class template argument; use typename to tell the compiler it is a type.
Changing KEY_STATE to typedef struct still causes this error and actually causes a lot more. There are no free parentheses or things in too many parentheses, that is the other thing the article suggests.
Can C++ struct have member functions? Yes, they can.
In C++, structs and classes are pretty much the same; the only difference is that where access modifiers (for member variables, methods, and base classes) in classes default to private, access modifiers in structs default to public.
. A Structure does not have a shared location for all of its members. It makes the size of a Structure to be greater than or equal to the sum of the size of its data members. A Union does not have a separate location for every member in it.
Structs are value types and can be used to create objects that behave like built-in types. Structs share many features with classes but with the following limitations as compared to classes. Struct cannot have a default constructor (a constructor without parameters) or a destructor.
In C, the name of the type is struct KEY_STATE
.
So you have to declare the second struct as
typedef struct _DEVICE_EXTENSION
{
WDFDEVICE WdfDevice;
struct KEY_STATE kState;
} DEVICE_EXTENSION, *PDEVICE_EXTENSION;
If you do not want to write struct
all the time, you can use a typedef declare KEY_STATE
similar to DEVICE_EXTENSION
:
typedef struct _KEY_STATE
{
/* ... */
} KEY_STATE;
There is no bool
type in C prior to C99.
Also, there is no type called KEY_STATE
when you do struct KEY_STATE
.
Try this instead:
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With