gpt4 book ai didi

c++ - C 和 C++ 中 struct 的区别

转载 作者:IT老高 更新时间:2023-10-28 22:22:50 43 4
gpt4 key购买 nike

我正在尝试将 C++ 结构转换为 C 但不断收到“未声明的标识符”? 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
};

我在另一个结构中使用了 KEY_STATE 类型的变量:

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

导致错误 C2061:语法错误:标识符 'KEY_STATE'

...在 KEY_STATE kState; 行上,如果有什么不同的话,我正在使用 WDK 编译器进行构建。这当然是在头文件中。我正在将 C++ WDM 驱动程序移植到 WDF 和 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.

将 KEY_STATE 更改为 typedef struct 仍然会导致此错误,实际上会导致更多错误。没有自由括号或括号中的东西太多,这是本文建议的另一件事。

最佳答案

在 C 中,类型的名称是 struct KEY_STATE

所以你必须将第二个结构声明为

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

如果不想一直写struct,可以使用类似于DEVICE_EXTENSION的typedef声明KEY_STATE:

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

关于c++ - C 和 C++ 中 struct 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1492735/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com