What does the compiler error “missing binary operator before token” mean?

后端 未结 4 1835
执念已碎
执念已碎 2020-11-30 09:40

I recently got the following error when trying to compile with gcc:

error: missing binary operator before token \"(\"

Web and SO

4条回答
  •  暖寄归人
    2020-11-30 10:27

    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.

    One common cause is the sizeof operator in an #if directive:

    For example:

      #define NBITS (sizeof(TYPE)*8)
      //later
      #if (NBITS>16)    //ERROR
    

    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.

    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:

    So if THIS is undefined:

    #if THIS == 0  //valid, true
    
    #if THIS > 0 //valid, false
    
    #if THIS() == 0  //invalid. ERROR
    

    Typos in your #if statement can also cause this message.

提交回复
热议问题