简体   繁体   English

语法错误:'常量'

[英]Syntax error: 'Constant'

I am trying to learn some WinAPI programming on C++ trough this tutorial.我正在尝试通过本教程在 C++ 上学习一些 WinAPI 编程。 http://www.winprog.org/tutorial/resources.html http://www.winprog.org/tutorial/resources.html

The author first defines a constant in 'resource.h':作者首先在'resource.h'中定义了一个常量:

#define IDI_MYICON 101

and then he uses it in the .cpp file:然后他在 .cpp 文件中使用它:

#include "resource.h"
IDI_MYICON ICON "my_icon.ico"

When I try to do that I get following error: error C2059: Syntaxfehler: 'Konstante' which translates into syntax error: 'Constant(the noun)' What am I doing wrong here?当我尝试这样做时,我收到以下错误:错误 C2059: Syntaxfehler: 'Konstante' 转换为语法错误:'Constant(the noun)' 我在这里做错了什么?

Edit: Ok, as it seems that was a very dumb mistake of me.编辑:好的,因为这似乎是我的一个非常愚蠢的错误。 Well deserved -reputation.当之无愧 - 声誉。 Thanks for replying!感谢回复!

and then he uses it in the .cpp file然后他在.cpp 文件中使用它

#include "resource.h" IDI_MYICON ICON "my_icon.ico"

This is the problem.这就是问题。 The above code needs to be in a .RC file instead, and that file then needs to be compiled using a resource compiler , not a C++ source code compiler .上面的代码需要在.RC ​​文件中,然后需要使用资源编译器而不是 C++源代码编译器来编译该文件。

This line:这一行:

#include "resource.h"

Is valid in a .cpp file.在 .cpp 文件中有效。 It is useful so the same IDI_MYICON define can be used when both creating the resource and when referring to the resource in source code.它很有用,因此在创建资源和在源代码中引用资源时可以使用相同的IDI_MYICON定义。

This line:这一行:

IDI_MYICON ICON "my_icon.ico"

is NOT valid in a .cpp file, only in a .rc file!在 .cpp 文件中无效,仅在 .rc 文件中有效!

IDI_MYICON ICON "my_icon.ico"

is same as

101 ICON "my_icon.ico"

which has no meaning and it is syntactically wrong.这没有任何意义,而且在语法上是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM