Download MetaTrader 5

Programming own collection class

To add comments, please log in or register
Post web links to other trader's signals and make profit
nanuqcz
34
nanuqcz 2015.02.14 10:43 

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áš

Ex Ovo Omnia
3156
Ex Ovo Omnia 2015.02.14 11:13  
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).

nanuqcz
34
nanuqcz 2015.02.14 12:45  

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.
nanuqcz
34
nanuqcz 2015.02.14 12:59  

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 :-(

Alain Verleyen
Moderator
30752
Alain Verleyen 2015.02.14 13:39  
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"
Ex Ovo Omnia
3156
Ex Ovo Omnia 2015.02.14 13:40  
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; 

whroeder1
14532
whroeder1 2015.02.14 14:06  
  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
Alain Verleyen
Moderator
30752
Alain Verleyen 2015.02.14 14:26  
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);
}
nanuqcz
34
nanuqcz 2015.02.16 22:22  

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.

Ex Ovo Omnia
3156
Ex Ovo Omnia 2015.02.17 04:34  

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;
}
nanuqcz
34
nanuqcz 2015.02.17 08:13  

Ovo: Thanks, it works :-)

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

12
To add comments, please log in or register