我使用switch进行消息判断总会出现这样的错误:
error C2360: initialization of 'setDlg' is skipped by 'case' label
         see declaration of 'setDlg'
errorC2361: initialization of 'inspectingDlg'is skipped by 'default' label
        see declaration of 'inspectingDlg'
下面是源代码BOOL CContralDlg::PreTranslateMessage(MSG* pMsg) 
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message==WM_KEYDOWN)
{
if(pMsg->wParam==VK_RETURN||pMsg->wParam==VK_ESCAPE)
{
return -1;
}
switch(pMsg->wParam)
{

           case VK_NUMPAD0 :
   SendMessage(WM_CLOSE,0,0);
   break;
  case VK_NUMPAD1 :
   Cinspectset* setDlg = new Cinspectset;
   setDlg->Create(IDD_DIALOG_inspectset);
   setDlg->ShowWindow(SW_SHOW);
   break;
 case VK_NUMPAD2 :
   Cinspecting* inspectingDlg = new Cinspecting;
   inspectingDlg->Create(IDD_DIALOG_inspect);
   inspectingDlg->ShowWindow(SW_SHOW);
   break;
  default:
  return  true; }
}
return CDialog::PreTranslateMessage(pMsg);
}
当我注释掉case vk_numpad2这一个判断和相应的源代码时,又会出现这样的问题:
error C2361: initialization of 'setDlg' is skipped by 'default' label
只有把case vk_numbpad2和default那段注释掉,才没有错误
这是什么原因亚?救救我

解决方案 »

  1.   

    setDlg()和inspectingDlg()是你自己的处理函数吧?你肯定在这二个处理函数中做了定义变量的事情,但在case中是不可以这样做的,否则就会报这样的错。如果是这样的话,把这二个函数中定义的局部变量定义成全局的(不要在函数内定义),就可以了。也许能帮上你。
      

  2.   

    每个case都加上{},否则不能定义变量;
      

  3.   

    Cinspectset* setDlg = new Cinspectset;
    要拿到case外面。
    case语句中间不允许声明变量。
      

  4.   

    setDlg,和inspectingdlg是我要建立无模式对话框用的,Cinspectset,Cinspecting是那个类,这样不能作吗?
      

  5.   


    用{}把case后面的东西括起来就能解决了,我前两天也碰到个这样的问题。