源代码:
if(a>9) {
goto RETURN;
}
......
const char * path = argc >= 4 ? argv[3] : "test";
......
RETURN:
return 0;
报错语句:
testswitch.cpp: In function ‘int main()’:
testswitch.cpp:9: error: jump to case label
testswitch.cpp:8: error: crosses initialization of ‘const char* path’
问题分析:
该问题是因为在源代码中使用了goto语句,而如果前面在条件语句中执行了goto语句,则如果RETURN中要使用path,那么path没有定义,所以会报错。
解决办法:
1 若path只是在局部使用,则将该部分用{}包起来,说明该语句为局部语句,则RETURN肯定不会调用该语句。
2 尽量不使用goto语句,使用return直接跳出也可以。