error C2360: initialization of 'k' is skipped by 'case' label

本文解析了在Visual C++中使用switch语句时,因变量初始化位置不当导致的编译错误C2360。阐述了如何通过合理放置初始化语句或使用语句块来避免此类错误。

在VC中使用switch语句时遇到“error C2360: initialization of 'k' is skipped by 'case' label”的编译错误。

msdn有下面的说明:  
compiler   error   c2360  
initialization   of   identifier   is   skipped   by   case   label  
   
the   specified   identifier   initialization   can   be   skipped   in   a   switch   statement.  
   
it   is   illegal   to   jump   past   a   declaration   with   an   initializer   unless   the   declaration   is   enclosed   in   a   block.  
   
the   scope   of   the   initialized   variable   lasts   until   the   end   of   the   switch   statement   unless   it   is   declared   in   an   enclosed   block   within   the   switch   statement.  
   
the   following   is   an   example   of   this   error:  
   
void   func(   void   )  
{  
        int   x;  
        switch   (   x   )  
        {  
        case   0   :  
              int   i   =   1;               //   error,   skipped   by   case   1      
              {   int   j   =   1;   }       //   ok,   initialized   in   enclosing   block  
        case   1   :  
              int   k   =   1;               //   ok,   initialization   not   skipped  
        }  
}  

在switch语句内定义一个变量的时候,如果不在一个语句块内,它是直到遇到switch的"}"才结束的。int i = 1;错误,错就错在它是以switch的"}"结束的,此时被case 1:语句跳过。int j = 1;它是遇到下面的"}"就结束了,因此正确。int k = 1;它虽然没有在一个语句块中,但它的下一个结束"}"正好就是switch的"}",不会被跳过,因此也正确。

所以,如果有在case内定义新变量,最好将该条case内的语句加上{}构成语句块,避免出错!

#include <windows.h> #include <gl/gl.h> #include <gl/glu.h> #include <math.h> #pragma comment(lib, "opengl32.lib") #pragma comment(lib, "glu32.lib") HDC hDC; HGLRC hRC; HWND hWnd; bool blink = false; int blinkTimer = 0; // 初始化OpenGL void InitGL() { glClearColor(0.0f, 0.7f, 1.0f, 1.0f); // 天空蓝背景 glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); } // 绘制海绵宝宝头部(立方体) void DrawHead() { // 黄色头部 glColor3f(1.0f, 1.0f, 0.0f); // 黄色 glutSolidCube(2.0f); // 使用GLUT的立方体 // 添加海绵孔洞效果 glColor3f(0.9f, 0.9f, 0.0f); for(int i=0; i<50; i++) { glPushMatrix(); glTranslatef((rand()%100)/100.0f-0.5f, (rand()%100)/100.0f-0.5f, 1.01f); glutSolidSphere(0.05f, 8, 8); glPopMatrix(); } } // 绘制眼睛(带眨眼效果) void DrawEyes() { // 左眼 glPushMatrix(); glTranslatef(-0.5f, 0.3f, 1.0f); glColor3f(1.0f, 1.0f, 1.0f); // 白眼白 glutSolidSphere(0.3f, 16, 16); // 眼珠 glTranslatef(0.0f, 0.0f, 0.1f); glColor3f(0.0f, 0.0f, 0.0f); // 黑色眼珠 if(blink) { // 眨眼状态 - 画一条线 glBegin(GL_LINES); glVertex2f(-0.2f, 0.0f); glVertex2f(0.2f, 0.0f); glEnd(); } else { glutSolidSphere(0.1f, 16, 16); } glPopMatrix(); // 右眼(同上) glPushMatrix(); glTranslatef(0.5f, 0.3f, 1.0f); glColor3f(1.0f, 1.0f, 1.0f); glutSolidSphere(0.3f, 16, 16); glTranslatef(0.0f, 0.0f, 0.1f); glColor3f(0.0f, 0.0f, 0.0f); if(blink) { glBegin(GL_LINES); glVertex2f(-0.2f, 0.0f); glVertex2f(0.2f, 0.0f); glEnd(); } else { glutSolidSphere(0.1f, 16, 16); } glPopMatrix(); } // 绘制鼻子 void DrawNose() { glPushMatrix(); glTranslatef(0.0f, 0.0f, 1.0f); glColor3f(1.0f, 0.5f, 0.0f); // 橙色鼻子 glutSolidSphere(0.2f, 16, 16); glPopMatrix(); } // 绘制嘴巴 void DrawMouth() { glPushMatrix(); glTranslatef(0.0f, -0.4f, 1.0f); glColor3f(0.0f, 0.0f, 0.0f); glBegin(GL_LINE_STRIP); for(float angle = 0; angle <= M_PI; angle += 0.1f) { glVertex2f(cos(angle)*0.6f, sin(angle)*0.3f); } glEnd(); glPopMatrix(); } // 绘制整个海绵宝宝 void DrawSpongeBob() { glPushMatrix(); glScalef(1.5f, 1.5f, 1.0f); // 稍微压扁 DrawHead(); DrawEyes(); DrawNose(); DrawMouth(); glPopMatrix(); } // 渲染场景 void Render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0); // 摄像机位置 // 添加一些旋转动画 static float angle = 0; angle += 0.5f; glRotatef(angle, 0, 1, 0); DrawSpongeBob(); SwapBuffers(hDC); } // 定时器回调(用于眨眼动画) void CALLBACK TimerProc(HWND hWnd, UINT message, UINT idTimer, DWORD dwTime) { blinkTimer++; if(blinkTimer > 100) { // 每100个时间单位眨眼一次 blink = true; if(blinkTimer > 105) { blink = false; blinkTimer = 0; } } Render(); } // 窗口过程 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_CREATE: hDC = GetDC(hWnd); PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, 24, 0,0,0,0,0,0,0,0,0,0,0,0,0, 32, 0,0, PFD_MAIN_PLANE, 0,0,0,0 }; SetPixelFormat(hDC, ChoosePixelFormat(hDC, &pfd), &pfd); hRC = wglCreateContext(hDC); wglMakeCurrent(hDC, hRC); InitGL(); SetTimer(hWnd, 1, 16, TimerProc); // 约60fps break; case WM_DESTROY: KillTimer(hWnd, 1); wglMakeCurrent(NULL, NULL); wglDeleteContext(hRC); ReleaseDC(hWnd, hDC); PostQuitMessage(0); break; case WM_SIZE: glViewport(0, 0, LOWORD(lParam), HIWORD(lParam)); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, (GLfloat)LOWORD(lParam)/(GLfloat)HIWORD(lParam), 0.1f, 100.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // 主函数 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASS wc = { CS_HREDRAW | CS_VREDRAW | CS_OWNDC, WndProc, 0,0, hInstance, NULL, LoadCursor(NULL, IDC_ARROW), (HBRUSH)(COLOR_WINDOW+1), NULL, "SpongeBobWindow" }; RegisterClass(&wc); hWnd = CreateWindow("SpongeBobWindow", "海绵宝宝3D动画 - VC6.0 OpenGL", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); MSG msg; while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }这段代码在vc6.0中运行后报错如下:\vcchucun\47\0609\1.c(26) : error C2065: 'glutSolidCube' : undeclared identifier d:\vcchucun\47\0609\1.c(35) : error C2065: 'glutSolidSphere' : undeclared identifier d:\vcchucun\47\0609\1.c(96) : error C2065: 'M_PI' : undeclared identifier d:\vcchucun\47\0609\1.c(97) : warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data d:\vcchucun\47\0609\1.c(97) : warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data d:\vcchucun\47\0609\1.c(170) : error C2360: initialization of 'pfd' is skipped by 'case' label d:\vcchucun\47\0609\1.c(151) : see declaration of 'pfd' d:\vcchucun\47\0609\1.c(178) : error C2360: initialization of 'pfd' is skipped by 'case' label d:\vcchucun\47\0609\1.c(151) : see declaration of 'pfd' d:\vcchucun\47\0609\1.c(187) : error C2361: initialization of 'pfd' is skipped by 'default' label d:\vcchucun\47\0609\1.c(151) : see declaration of 'pfd' Error executing cl.exe.
06-10
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值