Programming own collection class

 

Hello,

I want to create OrderCollection class, which represents list of Order instances and which I can iterate. I have this code:

class OrderCollection
{
    public:
        Order orders[];
        int pointer;
        int size;
       
        void OrderCollection()
        {
            pointer = -1;
            size = 0;
        }
       
        void OrderCollection(const OrderCollection& orderCollection)
        {
            ArrayCopy(orders, orderCollection.orders);
            pointer = orderCollection.pointer;
            size = orderCollection.size;
        }
       
        void add(Order& order)
        {
            size = size + 1;
            ArrayResize(orders, size);
           
            orders[(size - 1)] = order;    // ERROR: "'=' - structure have objects and cannot be copied"
        }
       
        Order next();
       
        Order prev();
       
        Order current();
       
        int key();
       
        void rewind();
};


But on selected line, I am getting an error: "'=' - structure have objects and cannot be copied". I don't understand this error and Google didn't help me. How can I add object to this array the right way?

Thanks for answers,
Michal Mikoláš

 
nanuqcz:

Hello,

I want to create OrderCollection class, which represents list of Order instances and which I can iterate. I have this code:

class OrderCollection
{
    public:
        Order orders[];
        int pointer;
        int size;
       
        void OrderCollection()
        {
            pointer = -1;
            size = 0;
        }
       
        void OrderCollection(const OrderCollection& orderCollection)
        {
            ArrayCopy(orders, orderCollection.orders);
            pointer = orderCollection.pointer;
            size = orderCollection.size;
        }
       
        void add(Order& order)
        {
            size = size + 1;
            ArrayResize(orders, size);
           
            orders[(size - 1)] = order;    // ERROR: "'=' - structure have objects and cannot be copied"
        }
       
        Order next();
       
        Order prev();
       
        Order current();
       
        int key();
       
        void rewind();
};


But on selected line, I am getting an error: "'=' - structure have objects and cannot be copied". I don't understand this error and Google didn't help me. How can I add object to this array the right way?

Thanks for answers,
Michal Mikoláš

Hello Michal,

your Order is probably defined as a struct containing objects or strings.  You cannot copy struct that contains pointers (string in struct is a pointer, too).

 

Yes, Order class look like this:

class Order
{
    public:
        int ticket;
        int magic;
        string comment;
       
        void Order()
        {
        }
       
        void Order(const Order& order)
        {
            ticket = order.ticket;
            magic = order.magic;
            comment = order.comment;
        }
};

but there is copy constructor, so copying instance of this class should be possible.
 

I am going crazy for it :-(

Now i tried:

class Order
{
    public:
        int ticket;
        int magic;
        string comment;
       
        void Order()
        {
        }
       
        void Order(const Order& order)
        {
            ticket = order.ticket;
            magic = order.magic;
            comment = order.comment;
        }
       
        Order clone()
        {
            Order order;
            order.ticket = ticket;
            order.magic = magic;
            order.comment = comment;
           
            return(order);
        }

};


class OrderCollection
{
    public:
        ...
       
        void add(Order& order)
        {
            size = size + 1;
            ArrayResize(orders, size);
           
            orders[(size - 1)] = order.clone();    // ERROR: "'=' - structure have objects and cannot be copied"
        }
       
        ...
};

and getting same error.

I don't want to copy anything. I just want to add object to an array :-(

 
nanuqcz:

I am going crazy for it :-(

...

and getting same error.

I don't want to copy anything. I just want to add object to an array :-(

Please use the SRC button to post code (not Code style).

It's either an mql4 bug or an undocumented restriction, unless we missed something.

That seems related to array of objects only :

      Order test=order;          // this is working
      orders[size-1]=order;      // ERROR: "'=' - structure have objects and cannot be copied"
 
These are copy statements:

 orders[(size - 1)] = order.clone();

 orders[(size - 1)] = order; 

 return(order); 

When you need to assign the object rather than copy, you have to work with object descriptors rather than objects themselves.

i. e.

Order* order = new Order(); 

Order* orders[];

Then the following statement turns to assignment rather than copy:

orders[(size - 1)] = order; 

 
  1.  ERROR: "'=' - structure have objects and cannot be copied"
    You have a copy constructor but no assignment operator:
    Order* Operator=(const Order& that){
       ticket  = that.ticket;
       magic   = that.magic;
       comment = that.comment;
       return GetPointer(this);
    }

  2. ArrayCopy(orders, orderCollection.orders);
    
    ArrayCopy - MQL4 Documentation
    Array of classes and structures containing objects that require initialization aren't copied
    Can't use copy/fill
 
WHRoeder:
  1. You have a copy constructor but no assignment operator:
  2. ArrayCopy - MQL4 Documentation
    Array of classes and structures containing objects that require initialization aren't copied
    Can't use copy/fill

Very interesting, thank you.

A little change to your code which doesn't compile :

Order* operator=(const Order& that){
   ticket  = that.ticket;
   magic   = that.magic;
   comment = that.comment;
   return GetPointer(this);
}
 

Very thanks you all :-)

Now I was able to create my own mini-framework.
Working example here: http://pastebin.com/dtcF5jsR (scroll down to "Expert advisor" part). I am open to all criticsm and tips for getting source code better ;-)

Thank you again.

 

This part is a bit weird (inconsistent) from my point of view. 

void add(Order& order)
{
.....
  orders[(size - 1)] = GetPointer(order);
}
 
Order* order = new Order();
....
orders.add(order);

 I would code it as 

void add(Order* order)
{
.....
  orders[(size - 1)] = order;
}
 

Ovo: Thanks, it works :-)

(new version: http://pastebin.com/giWEgnaP)

Reason: