Have to pass array as reference, but it's a variable I cannot pass as reference?

To add comments, please log in or register
qleaper
21
qleaper  

Help me get out of this conundrum:

In OnCalculate, I wish to pass the volume[] array to a custom function. However, I get the comical situation above; the custom function asks for arrays to be passed as reference, and yet OnCalculate doesn't allow me to pass volume[] as reference because it's a constant (and I can't change that). Anyone has an idea?

Carl Schreiber
Moderator
7244
Carl Schreiber  
I don't understand what you mean - can you provide the code that works and that with the error?
InvestSoft
15333
Janusz Trojca  

I'm not sure if I understand your problem, but you can easily pass volume array to a custom function in this way:

//+------------------------------------------------------------------+
//| 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[])
  {
//---
      example(volume);
//---
  }
  
void example(const long &volume[])
{
   
}
Stuart White
22
Stuart White  

Hi,

It compiles and I get no errors.  Maybe reboot your system.

qleaper
21
qleaper  
Carl Schreiber:
I don't understand what you mean - can you provide the code that works and that with the error?
There's no code that works, unfortunately.
Janusz Trojca:

I'm not sure if I understand your problem, but you can easily pass volume array to a custom function in this way:

That's what I'm doing right now, to no avail. I just get the error I mentioned.


Here's the abridged version...

double CustomFunction(int steps, long &volume[]) {

    (...)
}


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[]) {

    (...)

    Buffer[i] = CustomFnction(11, volume); // 'volume' - constant variable cannot be passed as reference
  }

   return(rates_total);
}

If I drop the & in CustomFunction, I get the "arrays are passed by reference only" error on top of the other one.

InvestSoft
15333
Janusz Trojca  

I think that you must define CustomFunction with "const":

double CustomFunction(int steps, const long &volume[])
qleaper
21
qleaper  
Janusz Trojca:

I think that you must define CustomFunction with "const":

...Yep, that was my oversight. Thanks a lot.
To add comments, please log in or register