dingmaotu / mql4-lib

MQL4/5 Foundation Library For Professional Developers
Apache License 2.0
544 stars 251 forks source link

Classes in collections #65

Closed mrhoga closed 1 year ago

mrhoga commented 1 year ago

Hi,

having little experience with MQL, I'm having difficulty using the collection classes with custom classes. I'm trying to store custom class instances in a Vector without success. This is one of the many variants I have tried so far:

#property strict
#property indicator_chart_window

#include <Mql\Collection\Vector.mqh>

class CSignal
{
    private:

        datetime _timestamp;
        string  _message;
        color   _color;

    public:

        datetime getTimestamp(){ return _timestamp; }
        string  getMessage(){ return _message; }
        color   getColor() { return _color; }
        string  getDisplayMessage()
        {
            MqlDateTime date;
            TimeToStruct(_timestamp, date);
            return StringFormat("%s : %02i/%02i %02i:%02i", _message, date.mon, date.day, date.hour, date.min);
        }

        CSignal() : _timestamp(0), _message(NULL), _color(clrNONE) {}
        CSignal(datetime timestamp, string message, color clr){ _timestamp = timestamp; _message = message; _color = clr; }
        CSignal(const CSignal & other) {
            _timestamp = other._timestamp;
            _message = other._message;
            _color = other._color;
        }
        ~CSignal(){}
};

Vector<CSignal> _signals;

int OnInit()
{
    for (int i=0; i<10; i++)
    {
        CSignal signal(TimeCurrent(), "This is a message", clrBlue);
        _signals.add(signal);
    }

    foreachv(CSignal, s, _signals)
        Print(s.getDisplayMessage());

    return(INIT_SUCCEEDED);
}

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(rates_total);
}

This gives me a bunch of errors:

⛔ : 'CSignal' - objects are passed by reference only (43,18)
⛔ : 'const' - objects are passed by reference only (29,21)
⛔ : 'const' - objects are passed by reference only (29,34)
⛔ : 'const' - objects are passed by reference only (30,19)
⛔ : 'CSignal' - objects are passed by reference only (137,26)
⛔ : 'const' - objects are passed by reference only (139,29)
⛔ : 'const' - objects are passed by reference only (145,31)
⛔ : 'const' - objects are passed by reference only (157,27)
⛔ : 'CSignal' - objects are passed by reference only (39,26)
⛔ : 'const' - objects are passed by reference only (40,29)
⛔ : 'CSignal' - objects are passed by reference only (43,37)
⛔ : 'CSignal' - objects are passed by reference only (46,32)
⛔ : 'CSignal' - objects are passed by reference only (50,27)
⛔ : 'CSignal' - objects are passed by reference only (53,30)
⛔ : 'CSignal' - objects are passed by reference only (100,26)
⛔ : 'const' - objects are passed by reference only (101,29)
⛔ : 'CSignal' - objects are passed by reference only (124,37)
⛔ : 'CSignal' - objects are passed by reference only (138,32)
⛔ : 'CSignal' - objects are passed by reference only (143,27)
⛔ : 'CSignal' - objects are passed by reference only (146,30)
⛔ : 'CSignal' - objects are passed by reference only (112,26)
 •      in template 'Iter<CSignal>' specified with [T=CSignal] (112,26)
⚠️ : possible use of uninitialized variable 's' (47,2)
⛔ : 'const' - objects are passed by reference only (39,30)
 •      in template 'GenericEqualityComparer<CSignal>' specified with [T=CSignal] (39,30)
⛔ : 'const' - objects are passed by reference only (39,43)
 •      in template 'GenericEqualityComparer<CSignal>' specified with [T=CSignal] (39,43)
⛔ : 'const' - objects are passed by reference only (40,28)
 •      in template 'GenericEqualityComparer<CSignal>' specified with [T=CSignal] (40,28)
⛔ : 'SafeDelete' - no one of the overloads can be applied to the function call (44,31)
 •    could be one of 2 function(s) (44,31)
 •      void SafeDelete(T*) (44,31)
 •      void SafeDelete(T) (44,31)
⛔ : 'CSignal' - objects are passed by reference only (195,26)
 •      in template 'VectorIterator<CSignal>' specified with [T=CSignal] (195,26)
⛔ : 'SafeDelete' - no one of the overloads can be applied to the function call (71,13)
 •    could be one of 2 function(s) (71,13)
 •      void SafeDelete(T*) (71,13)
 •      void SafeDelete(T) (71,13)
⛔ : 'SafeDelete' - no one of the overloads can be applied to the function call (117,22)
 •    could be one of 2 function(s) (117,22)
 •      void SafeDelete(T*) (117,22)
 •      void SafeDelete(T) (117,22)
⛔ : 'val' - objects are passed by reference only (125,35)
⛔ : 'm_array' - objects are passed by reference only (29,39)
⛔ : 'ArrayInsert' - cannot to apply template (125,13)
 •      see declaration of 'ArrayInsert' (125,13)
⛔ : '==' - illegal operation use (39,85)
⛔ : 'Hash' - no one of the overloads can be applied to the function call (40,66)
 •    could be one of 15 function(s) (40,66)
 •      int Hash(const string) (40,66)
 •      int Hash(const char) (40,66)
 •      int Hash(const bool) (40,66)
 •      int Hash(const uchar) (40,66)
 •      int Hash(const short) (40,66)
 •      int Hash(const ushort) (40,66)
 •      int Hash(const int) (40,66)
 •      int Hash(const uint) (40,66)
 •      int Hash(const long) (40,66)
 •      int Hash(const ulong) (40,66)
 •      int Hash(const float) (40,66)
 •      int Hash(const double) (40,66)
 •      int Hash(const datetime) (40,66)
 •      int Hash(const color) (40,66)
 •      int Hash(T*) (40,66)
⛔ : object of 'CSignal' cannot be returned, copy constructor 'CSignal::CSignal(const CSignal &)' not found (193,60)

[Error] Result: 34 errors, 1 warnings

I also tried

Vector<CSignal> * _signals = new Vector<CSignal>();
...
CSignal  * signal = new CSignal(TimeCurrent(), "This is a message", clrBlue);
_signals.add(signal);

but also without success.

I'm stuck and any help is greatly appreciated.

dingmaotu commented 1 year ago

For custom classes, using pointers to manage them in a collection. For example, use Vector<CSignal*> type:

Vector<CSignal*> _signals = new Vector<CSignal*>();
...
CSignal  * signal = new CSignal(TimeCurrent(), "This is a message", clrBlue);
_signals.add(signal);