C ++中具有QString类型的switch / case语句 [英] switch/case statement in C++ with a QString type
本文介绍了C ++中具有QString类型的switch / case语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在程序中使用switch-case,但是编译器给我这个错误:
I want to use switch-case in my program but the compiler gives me this error:
switch expression of type 'QString' is illegal
如何使用开关
带有 QString
?的语句?
How can I use the switch
statement with a QString
?
我的代码如下:
bool isStopWord( QString word )
{
bool flag = false ;
switch( word )
{
case "the":
flag = true ;
break ;
case "at" :
flag = true ;
break ;
case "in" :
flag = true ;
break ;
case "your":
flag = true ;
break ;
case "near":
flag = true ;
break ;
case "all":
flag = true ;
break ;
case "this":
flag = true ;
break ;
}
return flag ;
}
推荐答案
如何使用带有QString的switch语句?
How can I use the switch statement with a QString?
您不能。在C ++语言中, switch
语句只能与整数或枚举类型一起使用。您可以正式将类类型的对象放入 switch
语句中,但这仅意味着编译器将寻找用户定义的转换以将其转换为整数或枚举类型
You can't. In C++ language switch
statement can only be used with integral or enum types. You can formally put an object of class type into a switch
statement, but that simply means that the compiler will look for a user-defined conversion to convert it to integral or enum type.
这篇关于C ++中具有QString类型的switch / case语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文