在C和C结构之间的差异++ [英] Differences between struct in C and C++

查看:152
本文介绍了在C和C结构之间的差异++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个C ++结构转换为C,但不断收到未声明的标识符?请问c + +有指结构不同的语法?

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
};

我使用的另一种结构内部类型KEY_STATE的变量:

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;

结果错误C2061:语法错误:标识符'KEY_STATE

...就行 KEY_STATE KSTATE; 我的WDK编译器建设,如果有什么差别。这是理所当然的头文件。我移植的C ++ WDM驱动程序WDF和C

...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.

这是MSDN文章C2061

这是初始化可以用括号括起来。为了避免这个问题,附上声明符在括号或使其一个typedef。

An initializer may be enclosed by parentheses. To avoid this problem, enclose the declarator in parentheses or make it a typedef.

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

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结构还是引起了这个错误,实际上导致了很多。有太多的括号内没有免费的括号或事物,那就是文章建议其他的事情。

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.

推荐答案

在C,该类型的名称是结构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;

如果你不想写结构所有的时间,你可以使用一个typedef申报 KEY_STATE 类似 DEVICE_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;

这篇关于在C和C结构之间的差异++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆