C 和 C 中结构体之间的差异++
我正在尝试将 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。
初始值设定项可以用括号括起来。要避免此问题,请将声明符括在括号中或将其设为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 C 中,类型的名称是 struct KEY_STATE 。
所以你必须将第二个结构声明为
如果你不想一直写
struct
,你可以使用类似于DEVICE_EXTENSION
声明KEY_STATE
代码>:In C, the name of the type is
struct KEY_STATE
.So you have to declare the second struct as
If you do not want to write
struct
all the time, you can use a typedef declareKEY_STATE
similar toDEVICE_EXTENSION
:C99 之前的 C 语言中没有
bool
类型。此外,当您执行
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 dostruct KEY_STATE
.Try this instead:
您需要使用
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
withstruct KEY_STATE
. In C++ you can leave thestruct
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 asstruct KEY_STATE
您可以/应该对结构进行 typedef,这样您就不需要每次声明该类型的变量时都需要 struct 关键字。
现在您可以执行以下操作:
或(如您的示例所示):
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.
Now you can do:
or (as in the example you have) :
您必须使用“struct”关键字限定结构变量:
当您使用 DEVICE_EXTENSION 时,您不必使用“struct”,因为您正在单个复合语句中进行结构定义和 typedef。因此,如果您想以类似的方式使用 KEY_STATE,您可以对它执行相同的操作:
You have to qualify a struct variable with the 'struct' keyword:
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: