8

I have a code like this:

class MyClass
{
   private:
     static const int intvalue= 50;
     static const float floatvalue = 0.07f;
 };

in Visual studio 2010 and I am getting this error:

Myclasses.h(86): error C2864: 'MyClass::floatvalue : only static const integral data members can be initialized within a class

So how to initialize a static constant float in c++?

If I use constructor, every time that an object of this class is created, the variable is initialized which is not good.

apparently the code is compiled with GCC on Linux.

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
mans
  • 17,104
  • 45
  • 172
  • 321
  • It doesn't compile on GCC for me, but gives a very nice pointer: *error: ‘constexpr’ needed for in-class initialization of static data member ‘const float MyClass::floatvalue’ of non-integral type*. Of course, considering that `constexpr` isn't even in VS2012... – chris Jul 10 '13 at 09:08
  • possible duplicate of [C++ initialize static variables in class?](http://stackoverflow.com/questions/5019856/c-initialize-static-variables-in-class) – quetzalcoatl Jul 10 '13 at 09:09

3 Answers3

17

MyClass.h

class MyClass
{
   private:
     static const int intvalue = 50; // can provide a value here (integral constant)
     static const float floatvalue; // canNOT provide a value here (not integral)
};

MyClass.cpp

const int MyClass::intvalue; // no value (already provided in header)
const float MyClass::floatvalue = 0.07f; // value provided HERE

Also, concerning

apparently the code is compiled with GCC on Linux.

This is due to an extension. Try with flags like -std=c++98 (or -std=c++03, or -std=c++11 if your version is recent enough) and -pedantic and you will (correctly) get an error.

gx_
  • 4,690
  • 24
  • 31
  • Do I need to define intvalue in cpp file? – mans Jul 10 '13 at 09:12
  • Well that's not really needed if your program never takes its address (which could happen when passing by reference to const), but it's good practice (and if indeed the address is never needed then the compiler is likely to not allocate storage at all anyway). – gx_ Jul 10 '13 at 09:14
  • (And if you _want_ to prevent taking its address you can use an `enum` instead.) – gx_ Jul 10 '13 at 09:20
  • This is helpful for getting code to compile with both C++98 and C++11, as C++11 wants a constexpr. – Chance Jul 09 '14 at 19:02
1

Try the following.

In the header file, instead of your current statement write:

static const float floatvalue;

In the CPP file, write:

const float MyClass::floatvalue = 0.07f;
Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
0

You have to define them outside of the class, as follows:

const int MyClass::intvalue = 50;
const float MyClass::floatvalue = 0.07f;

Of course, this shouldn't be done in a header, or you will get a multiple instance error. With ints, you can fake them using enum {intvalue = 50};, that won't work with floats, though.

riv
  • 6,846
  • 2
  • 34
  • 63
  • 1
    There's no need to fake it with the enum trick anymore. C++ supports defining static const integral types in the class body, so everything that you can do with an enum, you can do with a `static const int` member. The only problem here is the `float`, which isn't an integral type. Please don't keep using the enum hack in C++, its day has come and gone. That's what constants are made for. – Cody Gray - on strike Jul 10 '13 at 09:26