液氦散热器安装图集好了但是不工作怎么办,害我

switch()的参数注意项(转)
我的图书馆
switch()的参数注意项(转)
1.switch参数类型:switch 后面的表达式不能跟double,float,long,String ,boolean,可以接int,short,byte,char!2.switch中定义变量问题:问题点:switch (a) { case 1: CString str="ABCDE"; //这句编译有错误。请问为什么? =》在switch里只能加{}来定义变量。&&&&&&&&&&&&&&& case 2:
.... }&& 回复1:switch (a) { case 1: { CString str="ABCDE"; //OK }
.... } 加上花括号就没事了。这是因为如果a为2的话变量初始化语句将不被执行, 回复2:switch 是一个单独的程序段。而case(不加{})就不是一个单独的程序段。看这个例子: int j=1; switch(j) { case 1: case 2:b=1; } 可以编译。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 ) {
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 } } 回复3:对于 switch (k) { case 1:
case 2: i=0; } 如果按照whjpn (常盘平) 的想法,那么会有问题发生: 如果 k=1,则没什么 如果 k=2, 则i是谁定义的?这个问题怎么解决? 所以对上述问题的修正办法是: 1.在switch外声明变量,这样case 2就不会有错误了 2.在switch中声明局部变量,局部变量的标志是用{} 即:case 1: { } 但是case 2就不能使用i了 另外&&& int k = 2;&&& switch(k)&&& {&&&&&&& case 0:&&&&&&&&&&&//OK&&&&&&&&&&& int j = 1;//Error&& 不能定义,&&&&&&&&&&&&&&&&&& case 1:&&&&&&&&&&& {&&&&&&&&&&&&&&& int i = 1;&& //加了{},则可以定义,但是是局部变量&&&&&&&&&&& }&&&&&&&&&&&&&&&&&& case 2:&&&&&&&&&&& i = 2; //OK&&&&&&&&&&& diag_printf("kkkkkkkkkkkkkkkkkk:i=%d\n",i);&&&&&&& default: ;&&& }&& 在编译时,case 0的i被当作switch内的变量,所以尽管k=2,i仍然被赋值为2&& 即switch变量不能够在case中声明并赋值
TA的最新馆藏[转]&[转]&[转]&[转]&[转]&Switch.cpp_VC++电子线路绘图程序源代码_图形处理_新兴源码下载
当前位置: >
> Switch.cpp
VC++电子线路绘图程序源代码.rarRES
loading...
// Switch.cpp: implementation of the CSwitch class.
// Download by
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "DrawChip.h"
#include "Switch.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
IMPLEMENT_SERIAL(CSwitch, CComponent,0)
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//##ModelId=3E18E0CB0034
CSwitch::CSwitch()
m_Rect = CRect(0,0,100,60);
m_RectLP = m_R
m_RectTracker.m_rect = m_R
m_RectTracker.m_nStyle = CRectTracker::resizeO
//##ModelId=3E18E0CB0035
CSwitch::~CSwitch()
//##ModelId=3E18E0CB0032
void CSwitch::SetType(CString &strType)
m_strType = strT
//##ModelId=3E18E0CA03E4
CString CSwitch::GetType()
return m_strT
//##ModelId=3E18E0CA03B0
void CSwitch::GetNumInNameSer(CObList *pObList, CObList *pNumSer, int *pMax)
*pMax = 0;
POSITION pos = pObList-&GetHeadPosition();
CObject *pO
CSwitch *pS
CString strN
CString strLastN
while (pos != NULL)
pObject = pObList-&GetAt(pos);
if (pObject-&IsKindOf(RUNTIME_CLASS(CSwitch)))
pSwitch = (CSwitch*)pO
strName = pSwitch-&GetName();
if ((strName.GetAt(0) == 'W') || (strName.GetAt(0) == 'w'))
//如果名称的第一个字母是W或w
strLastName = strName.Mid(1);
//判断余下的字符是否数字
if (IsNum(strLastName))
//余下的字符全部都是数字
i = atoi(strLastName);
int *p = new int(i);
pNumSer-&AddHead((CObject *)p);
if (i&(*pMax))
//余下的字符不全是数字
//忽略不记
//名称的第一个字母不是R或r
pObList-&GetNext(pos);
//##ModelId=3E18E0CA03AE
CString CSwitch::CalculateName(CObList *pObList)
CObList *pNumSer = new CObL
char buf[10];
GetNumInNameSer(pObList,pNumSer,&max);
//分析数字序列
for (int i=1;i&=i++)
if (IsInList(i,pNumSer))
itoa(i,buf,10);
//itoa(i,buf,10);
//删除链表
//TRACE("pNumSer-&Count() = %d\n", pNumSer-&GetCount());
while (pNumSer-&GetHeadPosition())
delete (int *)pNumSer-&RemoveHead();
delete pNumS
return "W"+ CString(buf);
//##ModelId=3E18E0CA03AC
void CSwitch::Serialize(CArchive &ar)
CComponent::Serialize(ar);
//需要序列化m_RectLP
if (ar.IsStoring())
ar&&m_Rect&&m_RectLP&&m_RectTracker.m_nStyle&&m_strT
//&&m_RectTracker.m_rect
ar&&m_Rect&&m_RectLP&&m_RectTracker.m_nStyle&&m_strT
//&&m_RectTracker.m_rect
//##ModelId=3E18E0CA0375
void CSwitch::Draw(CDC *pDC)
/*根据元器件对象的颜色和线宽属性值绘制*/
CString name = this-&GetName();
int width = m_Rect.Width() ;
int height = m_Rect.Height() ;
CPen newpen(PS_SOLID,GetLineWidth(),GetColor());
CPen *pOldPen = pDC-&SelectObject(&newpen);
pDC-&MoveTo(m_Rect.left,(int)(m_Rect.top + height*0.3));
pDC-&LineTo((int)(m_Rect.left+width*0.2),(int)(m_Rect.top+height*0.3));
pDC-&MoveTo((int)(m_Rect.left+width*0.2),(int)(m_Rect.top+height*0.3));
pDC-&LineTo((int)(m_Rect.left+width*0.5),(int)(m_Rect.top));
pDC-&Ellipse((int)(m_Rect.left+width*0.2),(int)(m_Rect.top+height*0.3-width*0.1)
,(int)(m_Rect.left+width*0.4),(int)(m_Rect.top+height*0.3+width*0.1));
pDC-&Ellipse((int)(m_Rect.left+width*0.6),(int)(m_Rect.top+height*0.3-width*0.1)
,(int)(m_Rect.left+width*0.8),(int)(m_Rect.top+height*0.3+width*0.1));
pDC-&MoveTo((int)(m_Rect.left+width*0.8),(int)(m_Rect.top+height*0.3));
pDC-&LineTo((int)(m_Rect.left+width),(int)(m_Rect.top+height*0.3));
pDC-&SetBkMode(TRANSPARENT);
pDC-&TextOut((int)(m_Rect.left+width*0.2),(int)(m_Rect.top+height*0.3-width*0.1),name);
pDC-&SelectObject(pOldPen);
//##ModelId=3E18E0CA0373
CSwitch::CSwitch(SWITCH Switch)
//根据结构体Switch在默认位置构造一个开关对象
//在粘贴时使用
SetColor(Switch.m_color);
SetLineWidth(Switch.m_iLineWidth);
SetRotateAngle(Switch.m_RotateAngle);
SetName(Switch.m_strName);
SetIsSelected(Switch.m_IsSelected);
m_strType = Switch.m_strT
//m_Rect = CRect(0,0,100,60);
m_Rect = CRect(,);
m_RectLP = m_R
m_RectTracker.m_rect = m_R
m_RectTracker.m_nStyle = CRectTracker::resizeO
//##ModelId=3E18E0CA0371
CSwitch::CSwitch(CPoint *pt)
/*由于需要把新生成的元器件对象的m_Rect左上角坐标设置为*/
/*鼠标点按处的坐标转换成逻辑坐标后的值,并且初始化m_Rect的右下角坐标*/
/*所以将这一系列的初始化操作加到了CSwitch(CPoint *pt)构造函数中*/
m_Rect.left = pt-&x;
m_Rect.top = pt-&y;
m_Rect.right = m_Rect.left
+ 2000; //100;
m_Rect.bottom = m_Rect.top -1000;
m_RectLP = m_R
m_RectTracker.m_rect = m_R
m_RectTracker.m_nStyle = CRectTracker::resizeO
//##ModelId=3E18E0CA0340
void CSwitch::CopySwToStrucSw(SWITCH &Switch)
lstrcpy(Switch.m_strClassType, "Switch");
Switch.m_color = this-&GetColor();
Switch.m_iLineWidth = this-&GetLineWidth();
Switch.m_IsSelected = this-&GetIsSelected();
lstrcpy(Switch.m_strType , this-&GetType());
Switch.m_RotateAngle = this-&GetRotateAngle();
lstrcpy(Switch.m_strName , this-&GetName());
| Copyright &
. All Rights Reserved .由于转自转载,作者不详,在此表示感谢@作者
1.switch参数类型:
switch 后面的表达式不能跟double,float,long,String ,boolean,可以接int,short,byte,char!
2.switch中定义变量问题:
switch (a)
CString str=&ABCDE&; //这句编译有错误。请问为什么? =》在switch里只能加{}来定义变量。
&&&&&&&&&&&&&&&
switch (a)
CString str=&ABCDE&; //OK
加上花括号就没事了。这是因为如果a为2的话变量初始化语句将不被执行,
switch 是一个单独的程序段。而case(不加{})就不是一个单独的程序段。看这个例子:
switch(j) {
case 2:b=1;
可以编译。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 )
switch ( x )
int i = 1; // error, skipped by case 1
{ int j = 1; } // OK, initialized in enclosing block
int k = 1; // OK, initialization not skipped
对于 switch (k)
如果按照whjpn (常盘平) 的想法,那么会有问题发生:
如果 k=1,则没什么
如果 k=2, 则i是谁定义的?这个问题怎么解决?
所以对上述问题的修正办法是:
1.在switch外声明变量,这样case 2就不会有错误了
2.在switch中声明局部变量,局部变量的标志是用{}
即:case 1:
但是case 2就不能使用i了
&&& int k = 2;
&&& switch(k)
&&&&&&& case 0:
&&&&&&&&&&&//OK
&&&&&&&&&&& int j = 1;//Error&& 不能定义,
&&&&&&&&&&&
&&&&&&& case 1:
&&&&&&&&&&& {
&&&&&&&&&&&&&&& int i = 1;&& //加了{},则可以定义,但是是局部变量
&&&&&&&&&&& }
&&&&&&&&&&&
&&&&&&& case 2:
&&&&&&&&&&& i = 2; //OK
&&&&&&&&&&& diag_printf(&kkkkkkkkkkkkkkkkkk:i=%d\n&,i);
&&&&&&& default: ;
&& 在编译时,case 0的i被当作switch内的变量,所以尽管k=2,i仍然被赋值为2
&& 即switch变量不能够在case中声明并赋值
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:359172次
积分:4893
积分:4893
排名:第5027名
原创:143篇
转载:47篇
评论:55条
文章:15篇
阅读:26861
(1)(1)(7)(4)(1)(1)(2)(5)(6)(1)(1)(3)(2)(2)(1)(5)(5)(2)(1)(3)(1)(3)(2)(16)(13)(5)(2)(7)(21)(11)(2)(2)(1)(13)(5)(16)(1)(5)(1)(1)(8)switch语句中()内可以用CString类的变量吗?char 的呢?
switch语句中()内可以用CString类的变量吗?char 的呢?
发布时间: 19:42:58
编辑:www.fx114.net
本篇文章主要介绍了"switch语句中()内可以用CString类的变量吗?char 的呢?
0",主要涉及到switch语句中()内可以用CString类的变量吗?char 的呢?
0方面的内容,对于switch语句中()内可以用CString类的变量吗?char 的呢?
0感兴趣的同学可以参考一下。
CString不可以.
char可以.(不是char*)可以用char,这个大家好象都试过,CString我试过。确实可以,不过,我都先define了一把。char&当然行,CString没试过,ahphone(《风云3》里头劫镖的):
&&define时设么意思呢?怎样做呢?好象switch(var)&只支持基本数据类型,并不支持string,数组及struct等当然不支持switch中只能用int型和char型,而不能用其它类型。
用define实际上是把一个字符串定义为一个整形,相当于枚举类型的用法。用过!可以!没问题!只限于VC中,非标准C!to&w12():能给出使用方法吗。w12(),你是如何实现的,有点怀疑。。。搞错了,对不起!
CString&str2="2";
switch(*(str2.GetBuffer(str2.GetLength())))
}还是的,我想不可能的,无论如何都不能如此用只能是数字或字符吧~
不支持字符串的可以的,我&try&过chen_jun_fen(不懂这行):
请指教,你是怎样做的?哦,不好意思,我弄错了,
我再试了一下,不能用的,抱歉,我弄错了
一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!
二、互相尊重,对自己的言论和行为负责。
本文标题:
本页链接:查看: 663|回复: 2
C++ switch和case的问题
阅读权限10
本帖最后由 MyTH 于
17:51 编辑
[C++] 纯文本查看 复制代码switch(messageheader.id)
case Fallen_ONLINE:
memcpy(&fallen,messageheader.data,sizeof(Fallen));
try{n=pDlg-&m_OnlineList.GetItemCount();}catch(...){}
pDlg-&m_OnlineList.InsertItem(n,inet_ntoa(in.sin_addr));
pDlg-&m_OnlineList.SetItemData(n,sClient);
pDlg-&m_OnlineList.SetItemText(n,1,fallen.szSystemVersion);
str.Format(&CPU:%d*%dHZ MEM:%dM&,fallen.dwCPU,fallen.dwNumofCpu,fallen.dwMemSize);
pDlg-&m_OnlineList.SetItemText(n,2,str);
pDlg-&m_OnlineList.SetItemText(n,3,&空闲&);
for(int i=0;i&n;i++)
if (pDlg-&m_OnlineList.GetItemText( i , 0 ) == pDlg-&m_OnlineList.GetItemText(n,0))
pDlg-&m_OnlineList.DeleteItem(n);
--pDlg-&m_OnlineC
CString strOS = pDlg-&m_OnlineList.GetItemText(n,1);
case Fallen_SPEED:
MESSAGEHEADER
memcpy(&messageheader,pData,sizeof(MSGHEAD));
for(int i=0;i&pDlg-&m_OnlineList.GetItemCount();i++)
if (pDlg-&m_OnlineList.GetItemData(i)==sClient)
char chIP[128]={0},chPort[128]={0},*ip=NULL;
char buf[2000];
ip=messageheader.//容易出现空指针
if(strstr(ip,&|&)!=NULL)
char nip[128]={0},nport[128]={0};
strncpy(chIP,ip,strcspn(ip,&|&));
ip=ip+strcspn(ip,&|&)+1;
strcpy(chPort,ip);
netsel.Format(_T(&%sMb/bps&), chIP);
pDlg-&m_OnlineList.SetItemText(i,5,netsel);
pDlg-&m_OnlineList.SetItemText(i,4,chPort);
case Fallen_HEART:
比如这样一段代码
然后我如果要自己再到switch里添加个case&&c++报错,例如:在case Fallen_HEART:前面加一句case Fallen_SPEED:
然后报Fallen_SPEED未定义,这个怎么办
不知如何使用,求解
是要依次在头文件枚举吗?
变量应该定义在switch以外。
发帖求助前要善用【】功能,那里可能会有你要找的答案;如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心】和【CB】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
阅读权限30
变量应该定义在switch以外。
发帖求助前要善用【】功能,那里可能会有你要找的答案;如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心】和【CB】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
阅读权限10
变量应该定义在switch以外。
好的,我已经知道了,还是谢谢你.
发帖求助前要善用【】功能,那里可能会有你要找的答案;如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心】和【CB】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
免责声明:吾爱破解所发布的一切破解补丁、注册机和注册信息及软件的解密分析文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
( 京ICP备号 | 京公网安备 87号 )
Powered by Discuz!
Comsenz Inc.

我要回帖

更多关于 水冷散热器安装 的文章

 

随机推荐