a declaration cannot have a label
1.问题
2.分析
经过一番查找,发现是编译器的自己解释的局限性所致,会产生误分析。
与之类似的常见现象,还有在使用switch时在case 后面申请变量会出现 error: a declaration cannot have a label 的提示。
从网上找到的答案如下:
Case statements are only ‘labels’. This means the compiler will interpret this as a jump directly to the label.The problem here is one of scope. Your curly brackets define the scope as everything inside the ‘switch’ statement. This means that you are left with a scope where a jump will be performed further into the code skipping the initialization. The correct way to handle this is to define a scope specific to that case statement and define your variable within it.
真是Kiss compiler's ass, make it happy!~
3.解决
-
对于图中的错误,只需要将变更定义提前即可。
-
对SWITCH-CASE语句,在CASE后面加括号就行,即
switch (c)
case 1:
{
int temp;
if(xxx) {
xxx;
}
}
4.应对
在使用GCC时,要将所有的warning转为error即可快速找出,即 -Wall,-Werror
在使用ARMCC时,就要每改动一个文件,就编译一次,确保保 0 warning , 0 error
。