error: crosses initialization of xxx IN switch case
|
The C++ standard says:
The cases in Just put all objects and variables initializations before your switch, and everything will be fine. Consider this code: switch(k){case1:int t =4;break;default:break;} It will cause a "crosses initialization" error, because it is possible to skip the initialization of t, but after that it will still be in scope, even though it was never created in the first place. Now consider this: switch(k){case1:{int t =4;}break;default:break;} Here, you will not have the error, because the variable is inside a block, and will die at the end of the block ( at the closing To fix the first case, you just need to do: int t =0;switch(k){case1: t =4;break;default:break;}
|
posted on 2013-06-20 10:34 zhangyz017 阅读(311) 评论(0) 编辑 收藏 举报