当前位置:   article > 正文

c/c++: 编译错误:error: expected expression before ‘)‘ token & expected ‘;‘, ‘,‘ or ‘)‘ before ‘=‘ token

expected expression before

简介

这个expected的模式是:在某个符号A之前,期望某些特定的符号B,将符号A之前的表达式表达完整;可以表达完整的期望符号是B。

error: expected ‘;’, ‘,’ or ‘)’ before ‘=’ token

如是纯C的话,函数参数没有默认值;如果设置上默认值
int f(int a = 3);
default.c:3:14: error: expected ‘;’, ‘,’ or ‘)’ before ‘=’ token
int f (int a =3);
^

有编译问题

如果C++的话就可以设置这个参数默认值。
这里的错误提示也是比较清楚,因为函数定义里肯定不期望有等号,也就是在等号之前,编译器判断出来是函数定义式,但是缺少一个符号将表达式完整表达出来;

error: expected expression before ‘)’ token

含义:在括号前期望一个表达式,意味着目前括号去没有表达式。可能的原因是函数调用时,在最后的括号前多了一个逗号。
579 /home/sb/log.h:198:35: error: expected expression before ‘)’ token, 在这里函数:log_print是一个可以接收可变参数的一个函数;
580 log_print(LOG, _data);
581 ^
582 …/route.c:1306:25: note: in expansion of macro ‘LOG’
583 LOG( 0,
584 ^

出现问题的源码,这里举了一个普通的函数,在可变参数的函数或者宏调用时,也需要特别注意。这里编译器已经遍历到2后面的逗号,期望后面是一个表达式,所以期望在后括号前面有一个表达式。

void fun1(int a, int b);
void function2(void)
{
fun1(1,2,);
}
  • 1
  • 2
  • 3
  • 4
  • 5

discr.c:11:11: error: expected expression before ‘)’ token
fun1(1,2,);
^

一个可能的原因

是变参函数,在GCC新版本编译时,之前的方式已经不适用,需要使用“##” 双井和来做处理。
14:01:41 …/a.h:375:33: error: expected expression before ‘)’ token
14:01:41 (char *) (_label), VA_ARGS) 、、、ISO C 标准的格式,但是新版本GCC 的CPP 预处理不允许这样。
14:01:41 ^
14:01:41 …/a.c:191:3: note: in expansion of macro ‘A’
14:01:41 A(dcf_event, dump_list[idx].addr, dump_list[idx].sz,

(char *) (_label), ##VA_ARGS),这样,如果后续的参数没有传递过来,就会将##号之前的逗号去除。

GNU CPP permits you to completely omit the variable arguments in this way. In the above examples, the compiler would complain, though since the expansion of the macro still has the extra comma after the format string.
To help solve this problem, CPP behaves specially for variable arguments used with the token paste operator, ‘##’. If instead you write #define debug(format, …) fprintf (stderr, format, ## VA_ARGS) and if the variable arguments are omitted or empty, the ‘##’ operator causes the preprocessor to remove the comma before it. If you do provide some variable arguments in your macro invocation, GNU CPP does not complain about the paste operation and instead places the variable arguments after the comma. Just like any other pasted macro argument, these arguments are not macro expanded.

另一个

宏调定义有参数;
实际使用时没有传进来参数
#define IP_CFG_LOCK(a)
{
if(a) { \ 、、这里使用时出现错误。
a->ip_cfg_lock = 0;
}
}

IP_CFG_LOCK(); //这里没有传进来参数;

缺括号的情况

init.c:13:15: error: expected ‘;’ before string constant
system “/bin/sudo /usr/sbin/sysctl”);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
init.c:13:90: error: expected statement before ‘)’ token

error: expected ‘)’ before ‘unsigned’

static void Call_printk(KM_OPAQUE, unsigned int vec_nr);
假如KM_OPAQUE 这个宏没有定义,就会出这个error。那为什么没有报缺少定义呢?确实也有
error: unknown type name ‘KM_OPAQUE’

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/336530
推荐阅读
相关标签
  

闽ICP备14008679号