请各位来解释一下

HighHill_Cen 2005-12-21 06:43:30
#include <iostream>
#include<string>
using namespace std;
void main()
{
string str = "test";
switch(str)
{
case "test":
cout<<"test"<<endl;
default:
cout<<"default"<<endl;
}
}
--------------------Configuration: bicen - Win32 Debug--------------------
Compiling...
test.cpp
D:\projects\bicen\test.cpp(12) : error C2450: switch expression of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' is illegal
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
D:\projects\bicen\test.cpp(13) : error C2051: case expression not constant
D:\projects\bicen\test.cpp(17) : warning C4065: switch statement contains 'default' but no 'case' labels
Error executing cl.exe.

bicen.exe - 2 error(s), 1 warning(s)
...全文
88 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
csucdl 2005-12-21
  • 打赏
  • 举报
回复
A_B_C_ABC(黄瓜儿才起蒂蒂) up
HighHill_Cen 2005-12-21
  • 打赏
  • 举报
回复
谢谢各位,受教了
dx30611 2005-12-21
  • 打赏
  • 举报
回复
在加一点
字符型也可以的
fiftymetre 2005-12-21
  • 打赏
  • 举报
回复
switch...case..的类型必须是整形...LZ是不是从.net转学C++的阿?
0黄瓜0 2005-12-21
  • 打赏
  • 举报
回复
switch(变量){...}


变量必须是整型

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧