How to get rid of the following Error...
-
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.
-
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.
-
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.
have you heard of the
if
keyword ? a switch can work only with integers, not strings...[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
have you heard of the
if
keyword ? a switch can work only with integers, not strings...[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
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
-
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.
hi, you should use integer or char instead of string in switch. like:: int a; switch(a) { case 1:{fun1(); break;} }
-
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
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]
-
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.
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