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

标签 c++ c windows winapi kmdf

我正在尝试将 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/

相关文章:

c++ - C++ 的标准映射插入语义的基本原理?

c - 为什么 Visual Studio 会忽略我项目中的 .h 文件?

.net - 在不停止服务的情况下更新 dll

c++ - 您能推荐一款适合新手的有趣的驱动程序应用程序吗?

linux - 恢复已清除的文件内容

windows - 以编程方式在登录时启动应用程序

c++ - 将 Mat 拆分为相同大小的 subMat

c++ - DirectX 多个 DLL 版本?

c++ - 断点在适用于 Linux 的 Eclipse Luna 的 CDT 8.4.0 上不起作用

c - 使用 realloc 函数时出错