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; ...
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!
ma[0].period = ma1_period; ma[0].shift = ma1_shift;
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…);
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
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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:
And these are the errors I get:
Where am I doing wrong?
Thanks