这两句代码有什么区别,为什么前者报错?

w7 2014-01-04 07:11:51
消息处理函数中:

	switch (uMsg)
{ case WM_PAINT:
PAINTSTRUCT ps;
HDC hDC = BeginPaint(hWnd, &ps); //有错:不能编译 error C2360: “hDC”的初始化操作由“case”标签跳过

TextOut(hDC, 0, 0, TEXT("HELLO"), 2);
EndPaint(hWnd, &ps);
break;

为什么把那一句分成两句就没可以编译了?
		HDC hDC;
hDC = BeginPaint(hWnd, &ps);
...全文
259 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
boylafong 2014-01-07
  • 打赏
  • 举报
回复
变量作用域 case下面没有{} 是不能定义的
rxguoblp 2014-01-07
  • 打赏
  • 举报
回复
对于switch-case;用{}包起来每一个case的处理代码并且不要漏掉"default:break;"是一个好习惯。
parachutes30 2014-01-07
  • 打赏
  • 举报
回复
变量的定义可能会被switch的case分句跳过去,别的case分句就看不到这个定义了,所以编译器不允许这样做。
paldier 2014-01-06
  • 打赏
  • 举报
回复
楼上已经很多人解释了,请逐行逐字的看几遍
w7 2014-01-05
  • 打赏
  • 举报
回复
加上{}是可以,但是为什么不加就不对呢,谁能用中文解释下?
hushoubo 2014-01-05
  • 打赏
  • 举报
回复
引用 3 楼 VisualEleven 的回复:
case语句下面用{}包起来即可。
正解....
worldy 2014-01-05
  • 打赏
  • 举报
回复
你是c代码,c代码的必须将变量定义放在函数的开头,不能使用 HDC hDC =...;的语法 必须HDC hDC ; hDC=...;
zhuyf87 2014-01-04
  • 打赏
  • 举报
回复
switch ( expression ) case constant-expression : statement An inner block of a switch statement can contain definitions with initializations as long as they are reachable — that is, not bypassed by all possible execution paths. Names introduced using these declarations have local scope. For example:
// switch_statement2.cpp
// C2360 expected
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
   switch( tolower( *argv[1] ) )
   {
       // Error. Unreachable declaration.
       char szChEntered[] = "Character entered was: ";
   
   case 'a' :
       {
       // Declaration of szChEntered OK. Local scope.
       char szChEntered[] = "Character entered was: ";
       cout << szChEntered << "a\n";
       }
       break;
   
   case 'b' :
       // Value of szChEntered undefined.
       cout << szChEntered << "b\n";
       break;
   
   default:
       // Value of szChEntered undefined.
       cout << szChEntered << "neither a nor b\n";
       break;
   }
}
Eleven 2014-01-04
  • 打赏
  • 举报
回复
case语句下面用{}包起来即可。
schlafenhamster 2014-01-04
  • 打赏
  • 举报
回复
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 } }
真相重于对错 2014-01-04
  • 打赏
  • 举报
回复
c++ 了解一下变量的作用域

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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