• Advertisement
Sign in to follow this  

static member variables in c++

This topic is 2636 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

In order to initialize a static member variable, why must the variable type be an integral type? Why can't we use say... floats? What's the deal here? I am using Visual Studio 2008. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
class CBlah
{
public:

static const int iA = 1; // Works
static const float fB = 1.0f; // Doesn't Work

};


The Error:
"error C2864: 'CBlah::fB' : only static const integral data members can be initialized within a class"

Share this post


Link to post
Share on other sites
Try


class CBlah
{
public:

static const int iA = 1; // Works
static const float fB;

};

float CBlah::fB = 1.0f;




[edit]
Keyword in the error message was within -
"error C2864: 'CBlah::fB' : only static const integral data members can be initialized within a class"

Share this post


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

  • Advertisement