1. 程式人生 > 其它 >VS +qt C++ 編寫case語句出現初始化操作由”case“標籤跳過問題

VS +qt C++ 編寫case語句出現初始化操作由”case“標籤跳過問題

技術標籤:Qt學習問題記錄c++qt

VS +qt C++ 編寫case語句出現初始化操作由”case“標籤跳過問題

最近在編寫程式碼時用到switch語句時出現“line1”的初始化操作由“case”標籤跳過問題。

 switch (type)
                {
                
                case 1:
                
                    xRegLine* line1 = static_cast<xRegLine*>(entities[i]);             
                    qDebug
() << line1->getData().startpoint.x; qDebug() << line1->getData().startpoint.y; qDebug() << line1->getData().endpoint.x; qDebug() << line1->getData().endpoint.y; qDebug() << line1-
>getData().regHeight; break; /**< Line */ case 2: xCircle* cirle = static_cast<xCircle*>(entities[i]); qDebug() << cirle->getData().radius; qDebug
() << cirle->getData().center.x; qDebug() << cirle->getData().center.y; break; }

在這裡插入圖片描述
上述的程式碼就會出現圖片中的錯誤,這是因為初始化的時候沒有用大括號包含起來,就會報該錯誤,解決辦法為:

 switch (type)
                {
                
                case 1:
                {
                    xRegLine* line1 = static_cast<xRegLine*>(entities[i]);             
                    qDebug() << line1->getData().startpoint.x;
                    qDebug() << line1->getData().startpoint.y;
                    qDebug() << line1->getData().endpoint.x;
                    qDebug() << line1->getData().endpoint.y;
                    qDebug() << line1->getData().regHeight;
                }
                    break;       /**< Line */
                case 2:
                {
                    xCircle* cirle = static_cast<xCircle*>(entities[i]);
                    qDebug() << cirle->getData().radius;
                    qDebug() << cirle->getData().center.x;
                    qDebug() << cirle->getData().center.y; 
                }
                    break;
               }

只需要將初始化程式碼用大括號包含起來問題就迎刃而解了。