Mql5 iniziatilize array of struct

 

Hello,

i am a beginner of mql language.

I am trying to initialize an array of structs but when I compile the program the system gives an error.

I'm definitely doing something wrong.

This is my code:

 struct MaStruct {
   int period;
   int shift;
   ENUM_APPLIED_PRICE applyTo;
   ENUM_MA_METHOD mode;
   int handle;
};
MaStruct ma[];
 int OnInit ()
  {

   ma = {
         {ma1_period, ma1_shift, ma1_applyTo, ma1_mode, ma1_handle},
         {ma2_period, ma2_shift, ma2_applyTo, ma2_mode, ma2_handle},
         {ma3_period, ma3_shift, ma3_applyTo, ma3_mode, ma3_handle},
         {ma4_period, ma4_shift, ma4_applyTo, ma4_mode, ma4_handle},
         {ma5_period, ma5_shift, ma5_applyTo, ma5_mode, ma5_handle},
   };
   return ( INIT_SUCCEEDED );
  }

And these are the errors I get:


Where am I doing wrong?

Thanks

 

Array initializers won't work with non-const expressions, you'll have to assign the values one by one.

Doesn't look very nice but that's how it is.

ArrayResize(ma,5);
ma[0].period = ma1_period;
ma[0].shift = ma1_shift;
...
 
lippmaje:

Array initializers won't work with non-const expressions, you'll have to assign the values one by one.

Doesn't look very nice but that's how it is.

Thank you very much!

 
lippmaje: Array initializers won't work with non-const expressions, you'll have to assign the values one by one.
ma[0].period = ma1_period;
ma[0].shift = ma1_shift;
Rather than initializing each item, create a method:
 struct MaStruct {
   int period;
   int shift;
   ENUM_APPLIED_PRICE applyTo;
   ENUM_MA_METHOD mode;
   int handle;
   void init(int p, int s, … int h){ period=p; shift=s; … handle=h; }
};
Then you can initialize your struct in one line each.
ma[0].init(ma1_period, ma1_shift…);
ma[1].init(ma2_period, ma2_shift…);
 
William Roeder:
Rather than initializing each item, create a method: Then you can initialize your struct in one line each.



how to initialize the structure or class when it contains a static integer,  double arrays and integer arrays?

I tried this code and it throws an error for the arrays.

no error for the static integer, is this the correct way for static  integers?



//+------------------------------------------------------------------+
//|                                                           ee.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_minimum 1
#property indicator_maximum 10
#property  indicator_buffers 1
int important_number=1;
struct MaStruct
{
 int                 member_period;
 static int          member_very_important_static_int;
 double              member_some_array_buffer[];
 int                 member_some_int_array[9];
 MqlRates            member_price_now;
//---

 struct structure_needed
 {
  double             member_some_array_buffer_in_struct[];
  int                member_some_int_array_in_struc[9];
 };

 structure_needed    member_my_structure_needed;
//---

 void                init
 (
  int period,
  const int very_important_static_int,
  double&some_array_buffer[],
  int&some_int_array[],
  MqlRates&price_now,
  structure_needed&my_structure_needed
 )
 {
  member_period=period;
  member_very_important_static_int=very_important_static_int;
  member_some_array_buffer=some_array_buffer;
  member_some_int_array=some_int_array;
  member_price_now=price_now;
  member_my_structure_needed=my_structure_needed;
 }
};
//+------------------------------------------------------------------+
int MaStruct::member_very_important_static_int=10+important_number;
static int some_static=10+important_number;
double some_array_buffer_ABC[];
int some_int_array_ABC[9];
MqlRates price_now_ABC;
structure_needed inside_struct_ABC;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
 SetIndexBuffer(0,some_array_buffer_ABC);
 SetIndexStyle(0,DRAW_HISTOGRAM);
 MaStruct instance_maStruct;
 instance_maStruct.init(11,some_static
                        ,some_array_buffer_ABC,some_int_array_ABC
                        ,price_now_ABC, inside_struct_ABC);
//---
 return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
//---
//--- return value of prev_calculated for next call
 return(rates_total);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

the errors are:

'member_some_array_buffer' - invalid array access 
'some_array_buffer' - invalid array access
'member_some_int_array' - invalid array access   
'some_int_array' - invalid array access  
4 errors, 0 warnings    


Reason: