大家帮忙看看原因何在

miningNET 2008-12-04 11:31:45
#include <iostream>
using namespace std;
void main()
{
int i;
cin>>i;
switch(i)
{
case 1:
int j=1;
cout<<j<<endl;
break;
case 2:
int k=2;
cout<<k<<endl;
break;
case 3:
cout<<"error!"<<endl;
break;
}
}
编译出错:
C:\Documents and Settings\MINING\桌面\jk\fg.cpp(13) : error C2360: initialization of 'j' is skipped by 'case' label
C:\Documents and Settings\MINING\桌面\jk\fg.cpp(10) : see declaration of 'j'
C:\Documents and Settings\MINING\桌面\jk\fg.cpp(17) : error C2360: initialization of 'k' is skipped by 'case' label
C:\Documents and Settings\MINING\桌面\jk\fg.cpp(14) : see declaration of 'k'
C:\Documents and Settings\MINING\桌面\jk\fg.cpp(17) : error C2360: initialization of 'j' is skipped by 'case' label
C:\Documents and Settings\MINING\桌面\jk\fg.cpp(10) : see declaration of 'j'
Error executing cl.exe.
...全文
59 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
miningNET 2008-12-06
  • 打赏
  • 举报
回复
谢谢大家!!
xzhangyu 2008-12-05
  • 打赏
  • 举报
回复
赞同楼上说的
hityct1 2008-12-05
  • 打赏
  • 举报
回复
int j=1;
int k=2; 都放在switch(i)前面。
xiangxuf 2008-12-05
  • 打赏
  • 举报
回复
up
wangkeyunanjing 2008-12-05
  • 打赏
  • 举报
回复
case 语句中不能定义变量,除非用{}括起来
zhouqing19871024 2008-12-05
  • 打赏
  • 举报
回复
我也遇到过这样的情况,好象在CASE语句里面不能定义变量,应该在switch语句之外定义。

64,318

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧