Case expression not constant?
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
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;
}
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?
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement