Function with adjustable declared arrays result in a "incompatible types" error

 


Greetings MQL4 coders,

First of all, to make this posting more readable I strongly simplified the provided sample code.
My EA has a function with 16 array inputs with a variable lenght, initialized by the ArrayResize function. The values from these arrays will be modified during this function call. I've implemented this with the following syntax:

double EAFunction(double& Array1[], ..., double& Array16[])
    {
    ...
    }

This function is called multiple times in my EA, but with different arrays, like this:

FunctionValue = EAFunction(ArrayA1[], ..., ArrayA16[]);


These arrays are global declared since they are in use in more than 1 function. I wasn't able to declare them also as double& type so I declared them as double. This -of course- results in a "incompatible types" error when I compile the code.

My question, is calling the function with it's original inputs like:

for (i = 1; i <= iSize; i++)
    {
    Array1[i] = ArrayA[i]; 
    ...
    Array16[i] = Array16A[i];
    }

FunctionValue = EAFunction(Array1[], ..., Array16[]) 
...the only way to solve this, or are there smarter (read: less computationally intensive read/write) solutions for this?

Thanks for any help!

Jim
 

leave away the [] when you call the function.

Also you don't have to copy the arrays before passing them, the function will directly accept any double array. Also the names of the arrays in the function declaration are only relevant inside the function and don't matter outside of it. In the following I name them foo[] and baz[], it does not matter how the arrays are called that I will later pass into the function, it will accept any double array, no matter which one, no matter how big.


int start(){
   double something;
   double some_array[];
   double other_array[];

   ArrayResize(some_array, 42);
   ArrayResize(other_array, 23);

   something = MyFunction(some_array, other_array);

}

double MyFunction(double& foo[], double& baz[]){
   int i;
   for (i=0; i<ArraySize(foo); i++){
      foo[i] = 666;
   }
   for (i=0; i<ArraySize(baz); i++){
      baz[i] = 999;
   }
   return(1);
}
 

I was in the assumption that brackets always were required to define an array, even within a function syntax, so thanks very much for your answer 7bit :)

Jim 

 
VirtualReal:

I was in the assumption that brackets always were required to define an array, even within a function syntax, so thanks very much for your answer 7bit :)

Jim

They are required for the declaration of the array and in the function declaration.

But they are not used when calling the function. Look for example at the built-in functions like ArraySize() or ArrayResize(), etc. They are all defined in the same way: They take a whole array as parameter and are called without the array brackets. When you refer to the array as a whole you leave them away, when you refer to a single element of the array you use something like foo[baz] (which will then of course evaluate to a single value and not an array anymore).