有下面语句
void testswitch(){
  CString test="hello";
  switch(test){
  case "hello":
    AfxMessageBox("yes");
    break;
  default:
    break;
  }
}
编译无法通过.编译错误信息
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
warning C4065: switch statement contains 'default' but no 'case' labels
从语法上分析,这有什么错误?最后一句警告,代码中明明有case和default,为什么还警告说没有?

解决方案 »

  1.   

    类型错误啊
    switch只能接受整型
      

  2.   

    自然会出错。switch(××××)的语法问题,括号里面的×××××应该是常量或者能够产生常量的语句。
    你这里面直接用CString作为分支,肯定不行。
      

  3.   

    MSDN 中查询错误代码:Compiler Error C2450
    switch expression of type 'type' is illegalThe specified switch expression evaluated to an illegal type.A switch expression must evaluate to an integral type or a class type that has an unambiguous conversion to an integral type.If the expression evaluates to a user-defined type, a conversion operator needs to be supplied.The following is an example of this error:class X
    {
    public:
       int i;
    } x;
    class Y
    {
    public:
       int i;
       operator int() { return i; }  // conversion operator
    } y;
    void main()
    {
       int j = 1;
       switch ( x )              // error, x is not type int
       {
     default:  ;
       }
       switch ( y )              // OK
       {
     default:  ;
       }
    }
    大概意思是 SWITCH 要用整形。
      

  4.   

    你确认你写的是C++程序么?C++中  case 后是不可以跟字符串的. 要跟一个数值
      

  5.   

    不好意思,更正下:switch只能接受整型和字符类型
      

  6.   

    要对字符串进行判断,用if,else较好
      

  7.   

    switch()里面只能跟C++内置的基本型,CString 是在某一个库中定义的。
      

  8.   

    现在已经明白了switch整型或字符型的常量表达式.还有两个小问题:
    1.zaodt在msdn中找到错误的代码解释,这是怎样找到的?
    2.zaodt的那个例子switch(x)是错误的,而switch(y)则是正确的.这个例子不是看得很明白,谁可以解释一下?
    谢谢了.
      

  9.   

    因为y有个operator int() { return i; } 
      

  10.   

    switch 可以只能接收int,char,单个字符或是整型
    你可以改成case "h":
    或者自己define hello 的对应串
      

  11.   


    1、你在MSDN中输入错误编号C2450就可以看到了
    2、因为Y类中有进行operator int() { return i; }  // conversion operator,返回的类型符合switch语句的要求。
      

  12.   

    switch只能接受整型,不能接受字符串的
      

  13.   

    switch 语句只接受 有序类型,楼主看书的仔细!
      

  14.   

    operator int() { return i; }要求int参数的地方,返回i
      

  15.   

    class Y
    {
    public:
       int i;
       operator int() { return i; }  // conversion operator
    } y;
    整型数应该是调用了一个方法之后返回的.但是switch(y)中的y是一个Y类的对象,怎么变成一个返回的整型数了?如果是switch(y.int())倒不难理解了,因为这个方法返回了一个整型数i,但是switch(y)这个表达式实在有点搞不懂.是不是这个表达式中的y在什么时候调用了那个返回i的方法?麻烦解释一下.
      

  16.   

    switch(y)时y发生了强制类型转换。
      

  17.   

    总值,switch的参数应该是一个小于等于32位的数,不管是否是有符号的。char型为8位,符合要求。
      

  18.   

    能否说得更详细点?为什么不见强制类型转换的操作符?
    还有几个问题:
    1.operator int() { return i; }是什么意思?
    2.这个方法在什么时候会被调用?
    3.operator为什么作为一个返回值类型来使用?
    4.int是一个关键词,为什么可以作为函数标识符?
      

  19.   

       operator int(){return i;}
       是C++运算符重载里的内容,叫做:类类型转换函数。
       其功能是将类Y的对象转换为类型为int的数据。
       在执行switch(y)语句时,调用该函数。
      

  20.   

    switch 里面只能用字符类型,整数类型。
      

  21.   

    switch() 中只能用整形变量,
      

  22.   

    学到了,特别是operator int()这里。强!
    另外想问个问题,是不是c++里的所有默认操作都可以用operator xx改写?
      

  23.   

    switch只能接受整型和字符类型
      

  24.   

    switch只能接受整型和字符类型
      

  25.   

    CString test="hello"; 
    不能放在SWITCH里面,拷贝出去就好了
      

  26.   

    你们为什么不看看CString类呢???看看它如何实现:在需要一个char*参数的地方也能传递进去
      

  27.   

    class Y 

    public: 
       int i; 
       operator int() { return i; }  // conversion operator 
    } y; 
    一个问题:switch(y)的时候会发生强制转换,但y原来的类型又是什么呢?更加通用一点的问法就是:类默认的返回类型是什么?