Basic help with generic data collections (CArrayList for instance)

 

Hi,

I am trying to use the CArrayList<T> list using a specific class of mine, that is not complex at all, but I am bumping into very basic errors which I did not mange to solve. 

Do you know of any libraries or source codes that use CArrayList or similar generic structures so I can read them and try to learn by example? The documentation for these do not provide any examples and I did not find any either here in the forum.


For instance, I was trying to code a very simple logic to test these generic collections and even this I did not manage to get working:

#include <Trade\Trade.mqh>
#include <Generic\ArrayList.mqh>

class Position {
        private:

        int                _ticket;
        ENUM_POSITION_TYPE _type;
        int                _volume;
        double             _price_open;

        public:

        Position(const Position &p) : _ticket(p._ticket), _type(p._type), _volume(p._volume), _price_open(p._price_open) { }

        Position(int ticket) {
                _ticket     = ticket;
                PositionSelectByTicket(_ticket);
                _type       = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
                _volume     = (int)                PositionGetDouble(POSITION_VOLUME);
                _price_open =                      PositionGetDouble(POSITION_PRICE_OPEN);
        }

        int ticket () { return _ticket; }
        ENUM_POSITION_TYPE type () { return _type; }
        int volume () { return _volume; }
        double price_open () { return _price_open; }

        double profit() {
                PositionSelectByTicket(_ticket);
                return PositionGetDouble(POSITION_PROFIT);
        }

        bool bought() {
                return _type == POSITION_TYPE_BUY;
        }

        bool sold() {
                return _type == POSITION_TYPE_SELL;
        }

};

Then I tried declaring a list these ways:

class Operate {
private:
        CArrayList <Position> *Positions;
        CTrade G_trade;
public:

        void Operate(ulong deviation = 20) {
                G_trade.SetTypeFilling(ORDER_FILLING_FOK);
                G_trade.SetDeviationInPoints(deviation);
                Positions = new CArrayList <Position>();
        }
        [...]
}

and

class Operate {
private:
        CArrayList <Position> Positions;
        CTrade G_trade;
public:

        void Operate(ulong deviation = 20) {
                G_trade.SetTypeFilling(ORDER_FILLING_FOK);
                G_trade.SetDeviationInPoints(deviation);
        }
        [...]
}


But I always get bunch of errors like:

errors



These are all certainly very basic errors, but I fear my C++ is kinda rusty nowadays and I did not find many examples to help me understand how I should be using MQL5's generics.

Could you point me in the right direction or give me some practical examples?


Thanks!

 
Use CArrayObj instead and make sure your classes inherit CObject
 
nicholish en:
Use CArrayObj instead and make sure your classes inherit CObject

Suspected I'd need to inherit some classes indeed.

Will try to, thanks!

 
Are the Generic Data Collections (CSortedSet<T> etc) not designed for custom classes?
Can T only be primative MT5 types like int, double etc? 
I thought the whole point of the Generic Data Collections library was to allow you to use your own classes in the collections, but I also just get these errors....
 
WorthyVII #:
Are the Generic Data Collections (CSortedSet<T> etc) not designed for custom classes?
Can T only be primative MT5 types like int, double etc? 
I thought the whole point of the Generic Data Collections library was to allow you to use your own classes in the collections, but I also just get these errors....

mql5 is limited in terms of generic code unfortunately.

You need to use pointers not objects.

Except that restriction the generic classes are working pretty well.