Sign in to follow this  
n0rmanfreak

problem with "static const"

Recommended Posts

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]

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Quote:
Original post by Bregma
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.

Quoted for being almost the right answer. That is how you should write it, but it's still just declarations, not definitions. Although most compilers seem to let you elide it, according to the final draft standard you still need the definitions too:
// statid.h
class StatID
{

public:

static const int A = 1;
static const int B = 2;
// ...

};

// statid.cpp
const int StatID::A;
const int StatID::B;
// ...
Σnigma

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