Errors, bugs, questions - page 96

 

I'm confused, please advise me.

I created a structure, filled it with data, created an array of structures.

How can I now pass an array to a function, but only for one element of the structure ??????????????

struct Str
  {
   int               a;
   int               b;
                     Str(){a=2;b=3;};
                    ~Str(){};
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
Str srt[10];
void OnStart()
  {
   Print("sum a=",OnStr(srt[].a));??????????????????
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnStr(const Str &x[].y)?????????????????????
  {
   int sum=0;
   for(int i=0;i<10;i++)
     {
      sum+=x[i].y;
     }
   return(sum);
  }
 

Suggestion to developers, if possible it would be good to perform standard operations with structures

not like this:

struct Str
  {
   int               a;
   int               b;
                     Str(){a=2;b=3;};
                    ~Str(){};
  };
Str x;
Str y;
void OnStart()
  {
   x.a+=y.a;
   x.b+=y.b;   
  }

but like this:

void OnStart()
  {
   x+=y;     
  }
Copying structures is a thing of the past.
 
Urain:

I'm confused, please advise me.

I created a structure, filled it with data, created an array of structures.

How to pass an array to a function, but only for one element of the structure ??????????????


You can't. And you don't need to.

Because - who prevents the summing function from summing up just that element?

And so the code was corrected to make it "kosher":

struct Str
  {
   int               a;
   int               b;
                     Str(){a=2;b=3;};
                    ~Str(){};
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
Str srt[10];
void OnStart()
  {
   Print("sum a=",OnStr(srt));??????????????????
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnStr(const Str &x[])?????????????????????
  {
   int sum=0;
   for(int i=0;i<10;i++)
     {
      sum+=x[i].y;
     }
   return(sum);
  }

Only something like ArraySize() should be used in reality instead of 10 in the for loop inside the OnStr() function. And instead of y in "sum+=x[i].y" sum the real data field defined in the structure (a or b, for example).

 
Urain:

Suggestion to developers, if possible it would be good to perform standard operations with structures

not like this:

but like this:

copying of structures is a thing of the past.

There is such a thing in C++. It's called "operator overloading". It's the programmer who defines how it should work using the corresponding function.

But this function Renat said "no". Bad luck, eh?

 
simpleton:

I can't. And there is no need to.

Because who prevents you from summing up exactly that element in the summation function?

So the code was corrected to "kosher":

Only something like ArraySize() should be used instead of 10 in the for loop inside OnStr() function. And instead of y in "sum+=x[i].y" sum the real data field defined in the structure (a or b, for example).

As always when I give examples the point flew away, but I'm not surprised it's probably my bony language.

And the gist is this: write a function of arithmetic mean and then simply by passing an array of structures to this function, get the structure of the arithmetic mean for each of the members of the structure.

The example is given for simplicity, but probably unsuccessful. But thanks for the clarifications in principle.

I guess we really should bang the developers to allow "operator overloading".

Документация по MQL5: Основы языка / Типы данных / Структуры и классы
Документация по MQL5: Основы языка / Типы данных / Структуры и классы
  • www.mql5.com
Основы языка / Типы данных / Структуры и классы - Документация по MQL5
 

yes i had to write a bunch of methods just to add, multiply, divide, two structures,

the most annoying thing is that we will have to write it all over again on other structures.

PS Besides methods give type of structure those in return the same structure we operate with so the nested call does not pass ???

result firstly needs to be saved in a temporary structure and then passed to the subsequent processing ?? it is not clear why it is done so.

PPS Although all parameters are const there is no possibility of changing non-existing variable.


PPPS And sorry, structures are passed by reference, how can you refer to a non-existent structure (apparently, it's time to rest).

 
int sl_pips = SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL);
Erroneous remark"possible loss of data due to type conversion"? (Build: 305)
Документация по MQL5: Основы языка / Типы данных / Приведение типов
Документация по MQL5: Основы языка / Типы данных / Приведение типов
  • www.mql5.com
Основы языка / Типы данных / Приведение типов - Документация по MQL5
 
Urain:

I'm confused, please advise me.

I created a structure, filled it with data, created an array of structures.

How to pass an array to a function, but only for one element of the structure ??????????????

struct Str
  {
   int               a;
   int               b;
                     Str(){a=2;b=3;};
                    ~Str(){};
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
Str srt[10];
void OnStart()
  {
   Print("sum a=",OnStr(srt[].a));??????????????????
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnStr(const Str &x[].y)?????????????????????
  {
   int sum=0;
   for(int i=0;i<10;i++)
     {
      sum+=x[i].y;
     }
   return(sum);
  }

simpleton:

You can't. And you don't need to.

Because - who prevents the summing function from summing up just that element?

So, the code is adjusted to "kosher":

Only something like ArraySize() should actually be used, not 10 in the for loop inside OnStr(). And instead of y in "sum+=x[i].y" sum the real data field defined in the structure (a or b, for example).

There are two ways (as far as I understand):

1. Pass the array itself, specifying the index of the record we are going to work with as an additional parameter.

In this case, we need control over the array dimensionality, and the function declaration would look something like this...

OnStr(const Str &x[], int Index=-1)

2. Pass the secondary array with the dimension of one record, respectively all the work on control and processing of the function results is held at the function call point.

The function declaration will then look something like this

OnStr(const Str &x[1])
Документация по MQL5: Операции с массивами / ArrayRange
Документация по MQL5: Операции с массивами / ArrayRange
  • www.mql5.com
Операции с массивами / ArrayRange - Документация по MQL5
 
EvgeTrofi:
Erroneous remark"possible loss of data due to type conversion"? (Build: 305)

You need to explicitly convert the result to the int type.

int sl_pips = (int)SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL);

PS

You may see a working example here - OrderSend()

But as I understand, this warning can be ignored in principle...

 

If return has a lot of brackets - it starts getting confused!?

Here, for example, is a function that returns a number from a string like "klsfd Step 2:

int GetStep(string text){
   string Right;
   int U = StringFind(text, "Step ");
   int End, result;
   if(U>=0){
      U=U+5;
      Right = StringSubstr(text, U);
      End = StringFind(Right, ".");
      result=int(MathRound(StringToDouble(StringSubstr(text, U, End-U))));
      return(result);
   }
   return(0);
}

How is it different from this one?

int GetStep(string text){
   string Right;
   int U = StringFind(text, "Step ");
   int End;
   if(U>=0){
      U=U+5;
      Right = StringSubstr(text, U);
      End = StringFind(Right, ".");
      return(int(MathRound(StringToDouble(StringSubstr(text, U, End-U)))));
   }
   return(0);
}

The second one will cause compile-time problems, but the first one won't.