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.

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().

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;
	}
}
commented: Or don't. -2
commented: Test your code before posting. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.