0

When I try to compile this:

void *BaseEnt::GetVar(char const *varname)
{
	char name = *varname;
	switch(name)
	{
	case "id":
		return (void*)id;
	case "name":
		return (void*)name;
	case "count":
		return (void*)count;
	default:
		return (void*)NULL;
	}
}

I get these errors:

------ Build started: Project: Test Classes, Configuration: Debug Win32 ------
Compiling...
classes.cpp
c:\documents and settings\tom\my documents\visual studio 2008\projects\test classes\test classes\classes.cpp(25) : error C2051: case expression not constant
c:\documents and settings\tom\my documents\visual studio 2008\projects\test classes\test classes\classes.cpp(27) : error C2051: case expression not constant
c:\documents and settings\tom\my documents\visual studio 2008\projects\test classes\test classes\classes.cpp(29) : error C2051: case expression not constant
c:\documents and settings\tom\my documents\visual studio 2008\projects\test classes\test classes\classes.cpp(33) : warning C4065: switch statement contains 'default' but no 'case' labels
Build log was saved at "file://c:\Documents and Settings\tom\My Documents\Visual Studio 2008\Projects\Test Classes\Test Classes\Debug\BuildLog.htm"
Test Classes - 3 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I thought that things like "id" were constant because they're a litteral?

Any help would be appreciated.

PS. If you need more information on my problem, feel free to ask.

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by slackingjuggalo
0

the param to a switch cannot be a string.
It can only be an integer. To achieve what u are trying use if-else statement alongwith strcmp().

-2

try this

void *BaseEnt::GetVar(char const *varname)
{
	char name = *varname;
	switch(name)
	{
	case 'id':                        //use ' not "
		return (void*)id;
	case 'name':                       //use ' not "
		return (void*)name;
	case 'count':                       //use ' not "  
		return (void*)count;
	default:
		return (void*)NULL;
	}
}
Votes + Comments
Test your code before posting.
Or don't.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.