how to give values to a double variable in header

I am writing a header in which I want to create and immediately give value to a double. What I programmed is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef ST_1_C_MN_H
#define ST_1_C_MN_H

#include "MaterialBase.h"

class cSt_1_C_Mn: public cMaterialBase {


///////////// Data ////////////////////

private:

//density at 20C

double den20 = 54.7;

...
the rest of the header
...


The compiler output is:

./headers/St_1_C_Mn.h:38: error: floating-point literal cannot appear in a constant-expression
./headers/St_1_C_Mn.h:38: error: ISO C++ forbids initialization of member âden20â
./headers/St_1_C_Mn.h:38: error: making âden20â static
./headers/St_1_C_Mn.h:38: error: ISO C++ forbids in-class initialization of non-const static member âden20â


Can anyone tell me what the mistake is and how I can get around this. Thank you.
Set the value in the constructor
Peter87,

Thanks very much! It works as you suggested!

Yo can put it there (in the declaration that is) only if it's const (as your compiler said) so you must initialize it when declare it:
const double den20 = 54.7;

Of course this makes sense just when you are NOT going to change the value in your program at some point
Last edited on
Topic archived. No new replies allowed.