C++ 初始化操作由 case 标签跳过
最近在编毕业设计的程序, 中间遇到不少细节问题,随时整理记录下来。
运行环境VS2008 C++ MFC程序
switch(nIDEvent)
{
case 1:
CString strTime ; //系统时间
CTime time = CTime::GetCurrentTime();
strTime = time.Format(_T("%Y-%m-%d,%H:%M:%S"));
SetDlgItemText ( IDC_STATIC_time, strTime );
break;
case 2:
if( led == 0)
{
led =1 ;
}
else
{
led = 0 ;
}
break;
}
当之有case1的时候运行正常, 加入了 case2后 ,出现错误:
错误 2 error C2360: “time”的初始化操作由“case”标签跳过
错误 3 error C2360: “strTime”的初始化操作由“case”标签跳过
经过查找发现,如果要在case里面定义变量,需要用括号括起来{}。
改为
case 1:
{
CString strTime ; //系统时间
CTime time = CTime::GetCurrentTime();
strTime = time.Format(_T("%Y-%m-%d,%H:%M:%S"));
SetDlgItemText ( IDC_STATIC_time, strTime );
}
break;
OK啦~~~
---------------------
原文:https://blog.csdn.net/zhliy0711/article/details/7663405