switch/case statement in C++ with a QString type(带有 QString 类型的 C++ 中的 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
如何将 switch 语句与 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.
这篇关于带有 QString 类型的 C++ 中的 switch/case 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有 QString 类型的 C++ 中的 switch/case 语句
基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
