导航
  • 主页
  • VC综合技术
  • 互联网技术
  • MFC AppLauncher
  • .NET 技术
  • 界面
  • 进程
  • 算法
  • 硬件/系统
  • 数据库
  • VC++技术资源

switch为什么不能用CString类型?

ByronLiu 2004-04-11 05:29:53
以下这段代码为什么会出错,不能用CString类型吗?

错误信息:
: error C2450: switch expression of type 'class CString' is illegal
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
: error C2051: case expression not constant

代码:
CString Schtext;

switch(Shztext)
{
case _T("名称"):

Schtext = "NoteText";
break;




请各位帮帮忙,Thank YOU!!!!
...全文
1530 点赞 收藏 18
写回复
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
mengjj 2004-04-12
Case 34个值可以这么做阿

int Case_con
if str1 = "abc"
Case_con = 1;
else if str1 = ""
Case_con = 2;
......
else if str1 = "xyz"
Case_con = 34;

switch(Case_con)
case 1
...
case 34


我覺得最好不要else,并且不該用'='而要用'==':
case_con=0;
if (str=="aaa") case_con=1;
if (str=="bbb") case_con=2;
...
if (str=="nnn") case_con=34;

switch (case_con){
case 1: ...
case 2: ...
...
case 34: ...
}
回复
rwdx 2004-04-12
switch 只能使用有序类型
回复
yintongshun 2004-04-12
switch是面向过程的,CString是对象,不能在switch中使用对象,C++中保留了许多C中好的方法,有的问题是从面向对象的思想来理解的。
回复
fieldwind 2004-04-12
是啊,前几天偶也郁闷了一回。
回复
subtop 2004-04-12
我也是在抱怨,为什么switch-case只能用整数呢,没有办法:(
回复
kuangjingbo 2004-04-11
用hash函数影射为整型试试
回复
kuangjingbo 2004-04-11
只能限制为整型,字符型和枚举型
回复
guanjinke 2004-04-11
只能用整型
回复
bluebohe 2004-04-11
ByronLiu (已在27日前结贴 为什么还扣信誉分) ??
我看一下你的信誉分变化纪录
回复
要不自己作一个一对一映射的表,用整型来代替字符串
回复
taianmonkey 2004-04-11
The C++ switch Statement
The C++ switch statement allows selection among multiple sections of code, depending on the value of an expression. The expression enclosed in parentheses, the “controlling expression,” must be of an integral type or of a class type for which there is an unambiguous conversion to integral type. Integral promotion is performed as described in Integral Promotions in Chapter 3.

The switch statement causes an unconditional jump to, into, or past the statement that is the “switch body,” depending on the value of the controlling expression, the values of the case labels, and the presence or absence of a default label. The switch body is normally a compound statement (although this is not a syntactic requirement). Usually, some of the statements in the switch body are labeled with case labels or with the default label. Labeled statements are not syntactic requirements, but the switch statement is meaningless without them. The default label can appear only once.

Syntax

case constant-expression : statement

default : statement

The constant-expression in the case label is converted to the type of the controlling expression and is then compared for equality. In a given switch statement, no two constant expressions in case statements can evaluate to the same value. The behavior is shown in Table 5.1.

Table 5.1 Switch Statement Behavior

Condition Action
Converted value matches that of the promoted controlling expression. Control is transferred to the statement following that label.
None of the constants match the constants in the case labels; default label is present. Control is transferred to the default label.
None of the constants match the constants in the case labels; default label is not present. Control is transferred to the statement after the switch 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. The following code fragment shows how the switch statement works:

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;
}

A switch statement can be nested. In such cases, case or default labels associate with the most deeply nested switch statements that enclose them. For example:

switch( msg )
{
case WM_COMMAND: // Windows command. Find out more.
switch( wParam )
{
case IDM_F_NEW: // File New menu command.
delete wfile;
wfile = new WinAppFile;
break;
case IDM_F_OPEN: // File Open menu command.
wfile->FileOpenDlg();
break;
...
}
case WM_CREATE: // Create window.
...
break;
case WM_PAINT: // Window needs repainting.
...
break;
default:
return DefWindowProc( hWnd, Message, wParam, lParam );
}

The preceding code fragment from a Microsoft Windows® message loop shows how switch statements can be nested. The switch statement that selects on the value of wParam is executed only if msg is WM_COMMAND. The case labels for menu selections, IDM_F_NEW and IDM_F_OPEN, associate with the inner switch statement.

Control is not impeded by case or default labels. To stop execution at the end of a part of the compound statement, insert a break statement. This transfers control to the statement after the switch statement. This example demonstrates how control “drops through” unless a break statement is used:

BOOL fClosing = FALSE;

...

switch( wParam )
{
case IDM_F_CLOSE: // File close command.
fClosing = TRUE;
// fall through

case IDM_F_SAVE: // File save command.
if( document->IsDirty() )
if( document->Name() == "UNTITLED" )
FileSaveAs( document );
else
FileSave( document );

if( fClosing )
document->Close();

break;
}

The preceding code shows how to take advantage of the fact that case labels do not impede the flow of control. If the switch statement transfers control to IDM_F_SAVE, fClosing is FALSE. Therefore, after the file is saved, the document is not closed. However, if the switch statement transfers control to IDM_F_CLOSE, fClosing is set to TRUE, and the code to save a file is executed.


--------------------------------------------------------------------------------
回复
lsp5i5j 2004-04-11
只能是整型和CHAR型,
C语法将自动HAR转成INT型
所以CHAR也可以
回复
1HelloWorld 2004-04-11
Case 34个值可以这么做阿

int Case_con
if str1 = "abc"
Case_con = 1;
else if str1 = ""
Case_con = 2;
......
else if str1 = "xyz"
Case_con = 34;

switch(Case_con)
case 1
...
case 34
回复
8412230 2004-04-11
对亚,只能是整形的
回复
vcforever 2004-04-11
只能是int类型或者char类型
回复
ByronLiu 2004-04-11
我要Case34个值,用if速度就差多了。
回复
bluebohe 2004-04-11
还是使用if(){}else{}吧
回复
只能整型
回复
发动态
发帖子
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
社区公告

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