how to initialize member array in Class Constructor method?

 

Dear Members

I am stuck as how to initialize member array of a Class with fixed values in constructor method.

please help me out.

//+----------------------------------------------------------------------------------------------------------+
//| CLASS:        CFibonacci
//| APPLICATION:  to define auxiliary / support methods
//+----------------------------------------------------------------------------------------------------------+
class CFibonacci
  {
private:
    double              m_FE_Multiplier[7];   // to store fixed FE_Level Values
  }
//+----------------------------------------------------------------------------------------------------------+
//| METHOD:       CFibonacci()
//| APPLICATION:  'default' constructor method
//+----------------------------------------------------------------------------------------------------------+
CFibonacci::CFibonacci(void)
  {
    m_FE_Multiplier[7] = {0.618,1.000,1.236,1.382,1.618,2.000,2.618}; // error '{' - expression expected        CFibonacci.mqh  55      26

  }
 
Don't you need to declare the constructor in the class declaration??
 
  1. You didn't declare your constructor in the class.

  2. m_FE_Multiplier[7] = {0.618…

    The initialization list only works when you declare an array. You can not use it at runtime.

  3. Options:
    1. Make it a static member.
      class CFibonacci
        {
      private:
          static const double m_FE_Multiplier[7];   // to store fixed FE_Level Values
        };
      static const double CFibonacci::m_FE_Multiplier[7] = {0.618,1.000,1.236,1.382,1.618,2.000,2.618};

    2. Make a array in the constructor and copy to the member array.
    3. Do it the hard way.
      CFibonacci::CFibonacci(void)
        {
          m_FE_Multiplier[0] = 0.618;
          m_FE_Multiplier[1] = 1.000;
          ⋮
      

 
William Roeder:
  1. You didn't declare your constructor in the class.

  2. The initialization list only works when you declare an array. You can not use it at runtime.

  3. Options:
    1. Make it a static member.

    2. Make a array in the constructor and copy to the member array.
    3. Do it the hard way.

Thanks William / Dominik

I did defined the constructor but shortened the code here for ease of reading.

William, I actually ended up using the hard way :).

following is the revised full code, please help where I should put ...

static const double CFibonacci::m_FE_Multiplier[7] = {0.618,1.000,1.236,1.382,1.618,2.000,2.618};

To my understanding, it is a method !!! outside the Class definition. But then I get error "m_FE_Multiplier' - redefinition; different type modifiers"

//+----------------------------------------------------------------------------------------------------------+
//| CLASS:        CFibonacci
//+----------------------------------------------------------------------------------------------------------+
class CFibonacci
  {
private:
  //--- private members: used in Drawing FiboExpansion
    long                m_ChartID;
    int                 m_SubWindow;
    static double       m_FE_Multiplier[7];   // to store fixed FE_Level Values
    string              m_FE_ObjName;
    color               m_FE_Clr;
    ENUM_LINE_STYLE     m_FE_LineStyle;
    int                 m_FE_LineWidth;
    bool                m_FE_IsBackground;
    bool                m_FE_IsSelection;
    bool                m_FE_IsRayLeft;
    bool                m_FE_IsRayRight;
    bool                m_FE_IsHidden;
    long                m_FE_ZOrder;

protected:
  //---
public:
  //--- Parametric Contstructor and default Destructor methods
              CFibonacci(void);
             ~CFibonacci(void);
  //---
    double    m_FE_LevelPrice[];    // to get calculated FE_Level Prices
    bool      Get_FE(const struct_IndexPrice &pMoveStart,const struct_IndexPrice &pMoveEnd,
                     const struct_IndexPrice &pMoveRetrace);
    bool      Draw_FE(const datetime pTime_MoveStart,const double pPrice_MoveStart,
                      const datetime pTime_MoveEnd,const double pPrice_MoveEnd,
                      const datetime pTime_MoveRetrace,const double pPrice_MoveRetrace);
  }; // END Of creating CLASS 'CFibonacci'

static const double CFibonacci::m_FE_Multiplier[7] = {0.618,1.000,1.236,1.382,1.618,2.000,2.618}; // 'm_FE_Multiplier' - redefinition; different type modifiers	CFibonacci.mqh	46	33

//+----------------------------------------------------------------------------------------------------------+
//| METHOD:       CFibonacci()
//| APPLICATION:  'default' constructor method
//+----------------------------------------------------------------------------------------------------------+
CFibonacci::CFibonacci(void)
  {
    m_ChartID          = 0;                 // default in Main Chart
    m_SubWindow        = 0;                 // subwindow index
    //m_FE_Multiplier[0] = 0.618;
    //m_FE_Multiplier[1] = 1.000;
    //m_FE_Multiplier[2] = 1.236;
    //m_FE_Multiplier[3] = 1.382;
    //m_FE_Multiplier[4] = 1.618;
    //m_FE_Multiplier[5] = 2.000;
    //m_FE_Multiplier[6] = 2.618;
    m_FE_Clr           = clrRed;            // object color
    m_FE_LineStyle     = STYLE_SOLID;       // style of the lines
    m_FE_LineWidth     = 1;                 // width of the lines
    m_FE_IsBackground  = false;             // display in the foreground (false) or background (true)
    m_FE_IsSelection   = true;              // highlight to move
    m_FE_IsRayLeft     = false;             // object's visualization to the left
    m_FE_IsRayRight    = false;             // object's visualization to the right
    m_FE_IsHidden      = false;             // true = hidden in the object list
    m_FE_ZOrder        = 0;                 // priority for mouse click
  } // END Of method Init_Parameters()...
 
Anil Varma -: To my understanding, it is a method !!! outside the Class definition. But then I get error "m_FE_Multiplier' - redefinition; different type modifiers"

Did I not show that in #2.3.1?

In your class you have static double. Outside you have static const double. What part of “different type modifiers” was unclear?

 
It is not a method. It's a member of type static.

Now if you want it to be a const type you need to have an assigning (defining) declaration.

You currently have a static declaration and a const static defining declaration.
One of them needs to be removed, obviously.






 
Dominik Egert:
It is not a method. It's a member of type static.

Now if you want it to be a const type you need to have an assigning (defining) declaration.

You currently have a static declaration and a const static defining declaration.
One of them needs to be removed, obviously.

How to achieve it ?

Would be great, if you can show me in the code above ...

Else I am going with the hard way for now :)

Thanks for your response and guidance.

 
William Roeder:

Did I not show that in #2.3.1?

In your class you have static double. Outside you have static const double. What part of “different type modifiers” was unclear?

Thanks William

I tried exactly the way you have suggested, but got error "m_FE_Multiplier' - redefinition; different type modifiers"

 

Like so:

class CFibonacci
  {
private:
   static const double       s_FE_Multiplier[]; // declare it inside the class
....
  };

static const double CFibonacci::s_FE_Multiplier[] = {0.618,1.000,1.236,1.382,1.618,2.000,2.618}; // ...then define it outside the class
 
lippmaje:

Like so:

Thank lippmaje

It worked, the difference is I was defining no of elements in array, where as in your case it was dynamic.

If possible let me know how this worked.

 
Anil Varma -:

Thank lippmaje

It worked, the difference is I was defining no of elements in array, where as in your case it was dynamic.

If possible let me know how this worked.

If you want a non-dynamic array you have to make sure to specify exactly the same number of elements that's used in the initializer list, otherwise you'll get this error.

class CFibonacci
  {
private:
   static const double       s_FE_Multiplier[1];
....
  };

static const double CFibonacci::s_FE_Multiplier[1] = {0.618,1.000,1.236,1.382,1.618,2.000,2.618}; // 's_FE_Multiplier' - redefinition; different type modifiers


To avoid that hassle I suggest to always go with dynamic arrays when it comes to initialize it by a list.

class CFibonacci
  {
private:
   static const double       s_FE_Multiplier[];
....
  };

static const double CFibonacci::s_FE_Multiplier[] = {0.618,1.000,1.236,1.382,1.618,2.000,2.618}; // compiles ok


The advantage is that you don't have to count your initializers and adapt that array size everytime you change that list.

The disadvantage is that a dynamic array uses up more memory - 52 bytes - for the dynamic array properties. I guess you can live with this in most of the cases.

Reason: