Sign in to follow this  

problem with "static const"

This topic is 4327 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Guest Anonymous Poster
use an enum.

Share this post


Link to post
Share on other sites
It is impossible to figure out what is wrong if you don't provide the relevant code or the error.

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

Share this post


Link to post
Share on other sites
Constants have to be initialized when they are declared.

Don't know if that is the only problem but it is A problem.

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
hi enigma!

as stated in my above post...this doesn't compile :(
it gives me errors C2258 and C2252. i'm using vc++ 6.0 with sp2

Share this post


Link to post
Share on other sites
Hey bud

It doesn't surprise that VS6 is giving you problems with this. It might be worth making the transition over toVS2005 now.

Dave

Share this post


Link to post
Share on other sites
hi Dave!

and thx for your quick answer...i just figured out something like that while asking google for some help ;)

thx all


edit: many typos :(

[Edited by - n0rmanfreak on March 14, 2006 10:59:08 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this