Create a CStack object that contains simple MqlRates objects

 

Hello,

I am a bit new to mql5. I have some C++ experience and I would like to create a stack which contains MqlRates.

I have checked the documentation and there is the CStack class from Generic Data Collection https://www.mql5.com/en/docs/standardlibrary/generic/cstack

I thought the declaration was simple, very much like this:

#include <Generic\Stack.mqh> 

CStack<MqlRates> rateStack;

However, this throws an error ( " - objects are passed by reference only Stack.mqh" ). I don't exactly understand how to implement this kind of queues and I didn't manage to find any explanation out there. Could you help me make it work?

Thanks!

Documentation on MQL5: Standard Library / Generic Data Collections / CStack
Documentation on MQL5: Standard Library / Generic Data Collections / CStack
  • www.mql5.com
The CStack class is a dynamic collection of T type data, which is organized as a stack that operates on the LIFO (last in, first out) principle.
 
Carles Balsells:

Hello,

I am a bit new to mql5. I have some C++ experience and I would like to create a stack which contains MqlRates.

I have checked the documentation and there is the CStack class from Generic Data Collection https://www.mql5.com/en/docs/standardlibrary/generic/cstack

I thought the declaration was simple, very much like this:

However, this throws an error ( " - objects are passed by reference only Stack.mqh" ). I don't exactly understand how to implement this kind of queues and I didn't manage to find any explanation out there. Could you help me make it work?

Thanks!

#include <Generic\Stack.mqh> 
MqlRates theRates[];


//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
    double ratesclose[];
    int ratesindex[];
    datetime ratestime[];
    ArrayResize(ratesclose,10);
    ArrayResize(ratesindex,10);
    ArrayResize(ratestime,10);
    ArrayResize(theRates,10);
    for (int i=0;i<10;i++)
    {
        theRates[i].close = 0.00+double(i);
        theRates[i].time = TimeCurrent()+i;
        ratesclose[i] = theRates[i].close;
        ratesindex[i] = i;
        ratestime[i] = theRates[i].time;
    }
    CStack<double> rateStackClose(ratesclose);
    CStack<int> rateStackIndex(ratesindex);
    CStack<datetime> rateStackTime(ratestime);

   //do the rest with the stack
   
  }
//+--


example only..simply Cstack does not accept MqlRates / struct, but array of data type..you could ZeroMemory the MqlRates structure to free the memory resource when you're done copying the MqlRates stucture members to array according to their data type and organize them back to MqlRates stucture in case you need it later when you're done with the stack, this is just one of the use cases

 
I will try this, thank you very much for the help!