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

270 篇文章 ¥9.90 ¥99.00

简介

这个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,);
}

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’

expected identifier or ‘(’ before ‘}’ token

expected declaration specifiers or ‘…’ before ‘(’ token
expected constructor, destructor, or type conversion before ‘(’ token
最近遇到一个错误是在宏定义函数里添加了一行注释,就出现了上面的错误。需要注意宏定义的函数如何加注释。需要用下面的注释形式

/* Find the maximum of two values */ \

!==

人为容易犯的一个错误就是上面这个符号,在C/C++里不存在。
所以也会报错误:

if(a!==0)
error: expected expression before ‘=’ token

在函数之外执行语句

#include <time.h>

    struct timespec ts;
//    ts.tv_sec = 0;
//    ts.tv_nsec = 1000000; // 1 millisecond


arp.c:17:7: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
     ts.tv_sec = 0;
       ^

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mzhan017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值