今天写程序遇到了一个问题,代码如下:编译时报错,d:/demo/vc编写的socket简单聊天程序/testserver/testserverdlg.cpp(245): error C2360: “is8001”的初始化操作由“case”标签跳过
switch (iMessageID)
{
case 8001:
InitSystem8001 *is8001=new InitSystem8001;
iRcvd = m_sConnectSocket.Receive(is8001,sizeof(InitSystem8001));
break;
case 8003:
AliveRequest8003 *ar8003 =new AliveRequest8003;
iRcvd = m_sConnectSocket.Receive(ar8003,sizeof(AliveRequest8003));
break;
}
看了一下网页,是说这个case 8001:语句可能没有被执行到(执行其他case了)导致变量没有初始化而导致接下来的错误,所以需要把变量放到switch前面声明初始化,可是我又不想把初始化放在switch前面,还有一个解决方法,在swith中定义内部变量加括号,就可以了。
switch (iMessageID)
{
case 8001:
{
InitSystem8001 *is8001=new InitSystem8001;
iRcvd = m_sConnectSocket.Receive(is8001,sizeof(InitSystem8001));
break;
}
case 8003:
{
AliveRequest8003 *ar8003 =new AliveRequest8003;
iRcvd = m_sConnectSocket.Receive(ar8003,sizeof(AliveRequest8003));
break;
}
}
时间: 2024-10-01 16:34:22