C 和 C 中结构体之间的差异++

发布于 2024-08-07 00:51:57 字数 1020 浏览 6 评论 0原文

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

这是C2061 的 MSDN 文章

初始值设定项可以用括号括起来。要避免此问题,请将声明符括在括号中或将其设为 typedef。

当编译器检测到表达式作为类模板参数时,也可能会导致此错误;使用 typename 告诉编译器它是一种类型。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忱杏 2024-08-14 00:51:57

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

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

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

如果你不想一直写struct,你可以使用类似于DEVICE_EXTENSION声明KEY_STATE代码>:

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

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;
旧人九事 2024-08-14 00:51:57

C99 之前的 C 语言中没有 bool 类型。

此外,当您执行struct KEY_STATE时,没有名为KEY_STATE的类型。

试试这个:

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;

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;
怪我入戏太深 2024-08-14 00:51:57

您需要使用struct KEY_STATE引用KEY_STATE。在 C++ 中,您可以省略 struct,但在普通 C 中则不行。

另一个解决方案是使用类型别名:

typedef struct KEY_STATE KEY_STATE

现在 KEY_STATE 与 struct KEY_STATE 含义相同

You need to refer to KEY_STATE with struct KEY_STATE. In C++ you can leave the struct out, but not in plain C.

Another solution is to do a type alias:

typedef struct KEY_STATE KEY_STATE

Now KEY_STATE means the same thing as struct KEY_STATE

手心的海 2024-08-14 00:51:57

您可以/应该对结构进行 typedef,这样您就不需要每次声明该类型的变量时都需要 struct 关键字。

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;

现在您可以执行以下操作:

KEY_STATE kState;

或(如您的示例所示):

struct KEY_STATE kState;

You could/should typedef the struct so that you don't need the struct keyword every time you will declare a variable of that 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;

Now you can do:

KEY_STATE kState;

or (as in the example you have) :

struct KEY_STATE kState;
自由范儿 2024-08-14 00:51:57

您必须使用“struct”关键字限定结构变量:

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

当您使用 DEVICE_EXTENSION 时,您不必使用“struct”,因为您正在单个复合语句中进行结构定义和 typedef。因此,如果您想以类似的方式使用 KEY_STATE,您可以对它执行相同的操作:

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;

You have to qualify a struct variable with the 'struct' keyword:

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

When you use DEVICE_EXTENSION you don't have to do use 'struct' because you are doing a struct definition and a typedef in a single compound statement. So you could do the same for KEY_STATE if you wanted to use it in a similar fasion:

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文