Sign in to follow this  
TheMan22

Case expression not constant?

Recommended Posts

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

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this