Case expression not constant?

Started by
8 comments, last by TheMan22 16 years, 2 months ago
I get errors case expression not constant, for this part of the code : switch(sAxis.asChar()) { case "X" : MGlobal::displayInfo("x"); break; case "Y" : MGlobal::displayInfo("y"); break; case "Z" : MGlobal::displayInfo("z"); break; } sAxis is a MString variable - maya's api string class This is maya api code,but a c++ programming issue so i am sure you guys can help. By the way MGlobal::displayInfo("x"); just means to print x in maya. thanks
Advertisement
single quotes instead of doubles.

switch(sAxis.asChar())
{
case 'X' :
MGlobal::displayInfo("x");
break;
case 'Y' :
MGlobal::displayInfo("y");
break;
case 'Z' :
MGlobal::displayInfo("z");
break;
}
Oh yes,i forgot single quotes for char.

so now there is one error left :

error C2450: switch expression of type 'const char *' is illegal

How to solve this?
Quote:sAxis.asChar()


What does this return? char or const char *?

One is a single character, the other is a string.
returns string in double quotes
But what is its return type? char, char *, const char *, something else?
const char* asChar () const

This is what it says in the docs
OK, then the error message tells you what's wrong: "switch expression of type 'const char *' is illegal".

You'll have to come up with a different solution, avoiding a switch according to the returned string. Maybe use a chain of if/else if/else or a data structure such as a map. Alternatively, if the returned string is always "X", "Y" or "Z", you could try a switch according to its first character.
throw a [0] on the end of that function call, if you just want the first character of the string.
Thanks

This topic is closed to new replies.

Advertisement