问题描述
我最近在尝试使用 gcc 进行编译时遇到以下错误:
I recently got the following error when trying to compile with gcc:
错误:在标记("之前缺少二元运算符
Web 和 SO 搜索提出了此错误的几个具体示例,并更改了特定代码以修复它们.但是我没有找到什么条件导致这个错误的一般描述.
Web and SO searches came up with several specific examples of this error, with specific code changes to fix them. But I found no general description of what condition causes this error to be issued.
gcc 何时以及为什么会发出此错误?
When and why does gcc emit this error?
推荐答案
这不是编译器错误,而是预处理器错误.当预处理器在尝试评估 #if
或 #elif
指令中的表达式时遇到无效语法时,就会发生这种情况.
This is not a compiler error, it is a preprocessor error. It occurs when the preprocessor encounters invalid syntax while trying to evaluate an expression in a #if
or #elif
directive.
一个常见的原因是 #if
指令中的 sizeof
操作符:
One common cause is the sizeof
operator in an #if
directive:
例如:
#define NBITS (sizeof(TYPE)*8)
//later
#if (NBITS>16) //ERROR
这是一个错误,因为 sizeof
是由编译器计算的,而不是预处理器.
This is an error because sizeof
is evaluated by the compiler, not the preprocesor.
类型转换也不是有效的预处理器语法:
Type casts are also not valid preprocessor syntax:
#define ALLBITS ((unsigned int) -1)
//later
#if (ALLBITS>0xFFFF) //ERROR
另请注意,#if
会将未定义的宏计算为 0,除非它看起来需要参数,在这种情况下,您 也会出现这个错误:
Note also that #if
will evaluate an undefined macro as 0, unless it looks like it takes arguments, in which case you also get this error:
所以如果 THIS
未定义:
#if THIS == 0 //valid, true
#if THIS > 0 //valid, false
#if THIS() == 0 //invalid. ERROR
#if
语句中的拼写错误 也可能导致此消息.
Typos in your #if
statement can also cause this message.
这篇关于编译器错误“在令牌之前缺少二元运算符"是什么意思?意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!