C++ 错误:宏名称必须是使用 #ifdef 0 的标识符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/426736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:17:27  来源:igfitidea点击:

Error: macro names must be identifiers using #ifdef 0

c++macrosc-preprocessor

提问by Eduardo

I have the source code of an application written in C++ and I just want to comment something using:

我有一个用 C++ 编写的应用程序的源代码,我只想使用以下方法评论:

#ifdef 0
...
#endif

And I get this error

我得到这个错误

error: macro names must be identifiers

错误:宏名称必须是标识符

Why is this happening?

为什么会这样?

回答by paxdiablo

The #ifdef directive is used to check if a preprocessor symbol is defined.The standard (C11 6.4.2 Identifiers) mandates that identifiers must not start with a digit:

#ifdef 指令用于检查是否定义了预处理器符号。标准 ( C11 6.4.2 Identifiers) 要求标识符不能以数字开头:

identifier:
    identifier-nondigit
    identifier identifier-nondigit
    identifier digit
identifier-nondigit:
    nondigit
    universal-character-name
    other implementation-defined characters>
nondigit: one of
    _ a b c d e f g h i j k l m
    n o p q r s t u v w x y z
    A B C D E F G H I J K L M
    N O P Q R S T U V W X Y Z
digit: one of
    0 1 2 3 4 5 6 7 8 9

The correct form for using the pre-processor to block out code is:

使用预处理器屏蔽代码的正确形式是:

#if 0
: : :
#endif

You can also use:

您还可以使用:

#ifdef NO_CHANCE_THAT_THIS_SYMBOL_WILL_EVER_EXIST
: : :
#endif

but you need to be confident that the symbols will notbe inadvertently set by code other than your own. In other words, don't use something like NOTUSEDor DONOTCOMPILEwhich others may also use. To be safe, the #ifoption should be preferred.

但是您需要确信符号不会被您自己以外的代码无意中设置。换句话说,不要使用类似NOTUSEDDONOTCOMPILE其他人也可能使用的东西。为了安全起见,该#if选项应该是首选。

回答by tvanfosson

Use the following to evaluate an expression (constant 0 evaluates to false).

使用以下内容来计算表达式(常量 0 的计算结果为 false)。

#if 0
 ...
#endif

回答by Fooo

This error can also occur if you are not following the marco rules

如果您不遵守 Marco 规则,也会发生此错误

Like

喜欢

#define 1K 1024 // Macro rules must be identifiers error occurs

Reason: Macro Should begin with a letter, not a number

原因:宏应该以字母开头,而不是数字开头

Change to

改成

#define ONE_KILOBYTE 1024 // This resolves 

回答by Deepak Reddy

#ifdef 0
...
#endif

#ifdef expect a macro rather than expression when using constant or expression

#ifdef 使用常量或表达式时期望宏而不是表达式

#if 0
...
#endif

or

或者

#if !defined(PP_CHECK) || defined(PP_CHECK_OTHER)
..
#endif

if #ifdef is used the it reports this error

如果使用#ifdef,它会报告此错误

#ifdef !defined(PP_CHECK) || defined(PP_CHECK_OTHER)
..
#endif

Where #ifdef expect a macro rather than macro expresssion

#ifdef 期望宏而不是宏表达式

回答by Drew Noakes

Note that you can also hit this error if you accidentally type:

请注意,如果您不小心键入,您也可能会遇到此错误:

#define <stdio.h>

...instead of...

...代替...

#include <stdio.>