Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to get rid of the following Error...

How to get rid of the following Error...

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pl_kode
    wrote on last edited by
    #1

    This is what I am doing... string str="MAX"; switch(str) { case "A":{ func1();} case "B":{ func2();} case "C":{ func3();} } following is the error I get.. error C2450: switch expression of type 'class std::basic_string,class std::allocator >' is illegal No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called Please help me resolve this... THANKS.

    A T M C 4 Replies Last reply
    0
    • P pl_kode

      This is what I am doing... string str="MAX"; switch(str) { case "A":{ func1();} case "B":{ func2();} case "C":{ func3();} } following is the error I get.. error C2450: switch expression of type 'class std::basic_string,class std::allocator >' is illegal No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called Please help me resolve this... THANKS.

      A Offline
      A Offline
      ahmad_ali
      wrote on last edited by
      #2

      You cannot switch on a string because the string is not an integral type. Integral means "simple" types such as int, long, short, char, ... You can achieve the same behavior using if ... else if if(str=="A") { } else if(str=="B") { } ...

      1 Reply Last reply
      0
      • P pl_kode

        This is what I am doing... string str="MAX"; switch(str) { case "A":{ func1();} case "B":{ func2();} case "C":{ func3();} } following is the error I get.. error C2450: switch expression of type 'class std::basic_string,class std::allocator >' is illegal No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called Please help me resolve this... THANKS.

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        have you heard of the if keyword ? a switch can work only with integers, not strings...

        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

        R 1 Reply Last reply
        0
        • T toxcct

          have you heard of the if keyword ? a switch can work only with integers, not strings...

          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #4

          toxcct wrote:

          a switch can work only with integers,

          Integral type, rather. :)

          Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

          T 1 Reply Last reply
          0
          • P pl_kode

            This is what I am doing... string str="MAX"; switch(str) { case "A":{ func1();} case "B":{ func2();} case "C":{ func3();} } following is the error I get.. error C2450: switch expression of type 'class std::basic_string,class std::allocator >' is illegal No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called Please help me resolve this... THANKS.

            M Offline
            M Offline
            Madan Chauhan
            wrote on last edited by
            #5

            hi, you should use integer or char instead of string in switch. like:: int a; switch(a) { case 1:{fun1(); break;} }

            1 Reply Last reply
            0
            • R Rajesh R Subramanian

              toxcct wrote:

              a switch can work only with integers,

              Integral type, rather. :)

              Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              indeed, but i didn't want to complexify my answer, as the OP doesn't seem to know C++, so i doubt he actually knows that a char is a small int !!! ;)

              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

              1 Reply Last reply
              0
              • P pl_kode

                This is what I am doing... string str="MAX"; switch(str) { case "A":{ func1();} case "B":{ func2();} case "C":{ func3();} } following is the error I get.. error C2450: switch expression of type 'class std::basic_string,class std::allocator >' is illegal No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called Please help me resolve this... THANKS.

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                if your string is actually composed by a single character then the switch construct is viable, for instance:

                string str="B";
                switch( str[0])
                {
                case 'A':
                {
                //...
                }
                break;
                case 'B':
                {
                //...
                }
                break;
                case 'C':
                {
                //...
                }
                break;
                default:
                {//no matching
                //...
                }
                break;
                }

                On the other hand, if you need to actually compare strings (i.e. more than 1 character involved) then you have to use a chain of if statements, for instance:

                string str = "foo";
                if ( str == "first option")
                {
                //...
                }
                else if (str == "second option")
                {
                //...
                }
                else if (str == "foo")
                {
                //...
                }
                else
                {//no matching
                //...
                }

                :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups