什么是编译器错误“在令牌之前缺少二进制算子”意思? [英] What does the compiler error "missing binary operator before token" mean?
问题描述
我最近遇到以下错误尝试使用gcc编译时:
I recently got the following error when trying to compile with gcc:
错误: p>
error: missing binary operator before token "("
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 seems to occur when the preprocessor encounters invalid syntax while trying to evaluate an expression in a #if
or #elif
directive.
一个常见的原因是 sizeof
指令
中的 / code>运算符例如:
For example:
#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
The rules for what can be in a valid expression are here.
请注意, #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.
这篇关于什么是编译器错误“在令牌之前缺少二进制算子”意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!