problem with "static const"

Started by
12 comments, last by n0rmanfreak 18 years, 2 months ago
hi! i wanna use a static const int for a switch statement. i declare it as shown in the following snippet. if i initialize the values within the header i get a linker error. if i initialize them in a .cpp the compiler tells me that a switch can only use const values.. any ideas ??? thx in advance

#ifndef _statid_h_
#define _statid_h_

class StatID
{
public:
	static const int A;		
	static const int B;
	static const int C;	
	static const int D; 
};

#endif  _statid_h_


[Edited by - n0rmanfreak on March 13, 2006 2:06:15 PM]
Advertisement
use an enum.
It is impossible to figure out what is wrong if you don't provide the relevant code or the error.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Hey bud,

There are two options here really:

1. In the header...
#ifndef _statid_h_#define _statid_h_const int HANDS;		const int FLOPSEEN;const int SHOWDOWN;	const int SHOWDOWN_WON;class StatID{public:};#endif  _statid_h_


2. An enum...
#ifndef _statid_h_#define _statid_h_enum { HANDS,FLOPSEEN,SHOWDOWN,SHOWDOWN_WON };		class StatID{public:};#endif  _statid_h_


Hope that helps mate,

Dave
A switch statement needs to be compiled with everything else. When compiling this switch statement, the values of the "cases" must be defined. It does not take the time to "look around" and see if the values are defined during the compilation :(

The only types that work with switch's are constants (27, 'a', etc.) and enumerations (enum's are basically named constants).

So using an enum would be your best bet.
-------Harmotion - Free 1v1 top-down shooter!Double Jump StudiosBlog
Quote:if i initialize the values within the header i get a linker error.


... did you #include your header?? Linker errors are a sign that you've done something wrong that's more basic than a code error.
Did you define your consts, or did you just declare them.
class StatID{public:    static const int A = 1;    static const int B = 2;};


That's defining them, not declaring them.

Stephen M. Webb
Professional Free Software Developer

Constants have to be initialized when they are declared.

Don't know if that is the only problem but it is A problem.
Declare them in your .h:

#ifndef _statid_h_
#define _statid_h_

class StatID
{
public:
static const int A;
static const int B;
static const int C;
static const int D;
};

#endif _statid_h_


Initialize them in your .cpp:

#include statid.h

//...other includes

// Initialize in the global class scope
const int StatID::A = 0;
const int StatID::B = 54;
const int StatID::C = 69;
const int StatID::D = 24;

StatID::StatID()
{
}

//...other member functions


Hope that helps...
hi!

thx all for your help ...i guess i wasn't clear enough on my first post :(

i tried two versions which both failed for different reasons:

1) code like the AP posted. declare the static const ints within the header and initalize them within the corresponding cpp

--> the compiler gives me an error stating that my case mark isn't a constant

2) declare the static const ints within the header and initialize them in the same file (after the class definition ends; code like bregmas fails cause the compiler tells me that only functions can be declared pure virtual)

--> the linker gives me an error telling me that StatID::A is already definded in a different obj (sure each time the header gets included it will add the definition)

here is my code for the switch:
void PlayerData::increaseStat(int statID){	switch(statID)	{	case StatID::A:		a++;		break;	case StatID::B:		b++;		break;	case StatID::C:		c++;		break;	case StatID::D:		d++;		break;	default:		// unkown statID		break;	}}


i wanna use static const and not define so i always _see_ which values are really defined and i easly can add more switch marks .


thx in advance

This topic is closed to new replies.

Advertisement