case语句中定义变量要加括号

map::iterator it;
	map::iterator itID;
	recv(AcceptSocket,(char*)&RecvBuf,sizeof(RecvBuf),0);
	switch(RecvBuf.Protocol)
	{	
	case REGISTE:
		map::iterator it;//报错:it由“case”标签跳过
		for(it=m_mUserPassword.begin();it!=m_mUserPassword.end();it++)
		{
			if(it->first==RecvBuf.SourUser)
			{
				SendBuf.Protocol = REGISTE_FAIL;
				send(AcceptSocket,(char*)&SendBuf,sizeof(SendBuf),0);
				break;
			}
		}
		if(it == m_mUserPassword.end())
		{
			m_mUserPassword[(LPTSTR)RecvBuf.SourUser] = (LPTSTR)RecvBuf.Password;
			SendBuf.Protocol = REGISTE_SUCC;
			send(AcceptSocket,(char*)&SendBuf,sizeof(SendBuf),0);
			UpdateData(1);
			m_editInfo += "\r\n";
			m_editInfo += RecvBuf.SourUser;
			m_editInfo += "注册成功";
			UpdateData(0);
							
		}
		break;				
	case LOAD:
		it = m_mUserPassword.find(RecvBuf.SourUser);
		if(it == m_mUserPassword.end()||it->second != RecvBuf.Password)
		{
			SendBuf.Protocol = LOAD_FAIL;
			send(AcceptSocket,(char*)&SendBuf,sizeof(SendBuf),0);
			str.Format("%s\r\n%s 验证失败",m_editInfo,RecvBuf.SourUser);
			m_EditInfo.SetWindowTextA(str);
		}
		else
		{
			SendBuf.Protocol = LOAD_SUCC;
			OnLineUser += RecvBuf.SourUser;
			OnLineUser += ",";
			strcpy_s(SendBuf.Msg,OnLineUser.GetBuffer(0));
			send(AcceptSocket,(char*)&SendBuf,sizeof(SendBuf),0);
			m_mUserSocket[RecvBuf.SourUser]=AcceptSocket;
			str.Format("%s\r\n%s 验证成功",m_editInfo,RecvBuf.SourUser);
			m_EditInfo.SetWindowTextA(str);
		}			
		break;
	case TOALL:
		for(itID=m_mUserSocket.begin();itID!=m_mUserSocket.end();itID++)
		{
			if(itID->first!=RecvBuf.SourUser)
			{
				send(itID->second,(char*)&RecvBuf,sizeof(RecvBuf),0);
			}
		}
		break;
	case TOSINGLE:
		send(m_mUserSocket[RecvBuf.DestUser],(char*)&RecvBuf\
			,sizeof(RecvBuf),0);					
		break;
	default:
		break;
	}

解决办法:

1,case语句加括号

2,将变量放在switch语句之前定义

比较:方法二较好,因为可以减少缩进,不至于让代码整体向后缩进影响外观和可读性。

你可能感兴趣的:(C++,调试,点滴)