How do I return array list of a class?

 

Hi all,

I'm just starting to code using mql4 and encountered an error stating " object of 'Order' cannot be returned, copy constructor 'Order::Order(const Order &)"  on POrder class. What should I do to fix it?

Best, 

class Order {
   private:
      int ticket;
      bool isOpened;
   
   public:
      Order (int ticket, bool isOpened) {
         this.ticket = ticket;
         this.isOpened = isOpened;
      }
                
      int getTicket() {
         return ticket;
      }
      
      bool getStatus() {
         return isOpened;
      }
            
      void setStatus(bool isOpened) {
         this.isOpened = isOpened;
      }
};
class POrder {
   
   private:
      int ticket;
      Order *orders[5];
   
   public:      
      POrder (int ticket, int& waiting[]) {
         this.ticket = ticket;
         for (int i = 0; i < ArraySize(orders) ; i++) {
            orders[i] = new Order(waiting[i], false);
         }
      }
               
      int getTicket() {
         return ticket;
      }
      
      Order getOrders(){ // the function that is causing the ERROR
         return(orders);  // why can't I return the array
      }
};
 
M1cha31: encountered an error stating " object of 'Order' cannot be returned, copy constructor 'Order::Order(const Order &)"  on POrder class. What should I do to fix it?
  1.       Order *orders[5];
    :
          Order getOrders(){ // the function that is causing the ERROR
             return(orders);  // why can't I return the array
          }
    A) You can not return an array. B) your function definition says it returns an Order, not a pointer to an Order.
  2. int& waiting[]
    You already know how to pass by reference. Pass an array and fill it.
  3. Or you can return a pointer to a class,
  4. Or if you want your class to be copyable create a copy constructor.
 

MQL function cannot return an array. 

You can return a pointer to the single object of the Order type

Order* getOrders(){          // Note "*" after the Order. You can return "pointer" to object
         return(orders[0]);  // You can return single object of the Order type
}

 

If you want to return an array of objects, you can use CArrayObj class. It's located in the include file <Arrays\ArrayObj.mqh>.

 

Alternative is to send an empty array as a parameter and then fill it with objects:

// this code is not tested
void getOrders(Order &ords[]){  
         ArrayCopy(ords, orders, 0, 0);
      }
 
WHRoeder:
M1cha31: encountered an error stating " object of 'Order' cannot be returned, copy constructor 'Order::Order(const Order &)"  on POrder class. What should I do to fix it?
  1. You can not return an array.
  2. You already know how to pass by reference. Pass an array and fill it.
  3. You can return a pointer to a class, or if you want your class to be copyable create a copy constructor.

Hi,

Thanks for the feedback. Can you give me snapshots (or link to a resources) that explains how to pass pointer to a class and creating a copy-able constructor. 

 
drazen64:

MQL function cannot return an array. 

You can return a pointer to the single object of the Order type

 

If you want to return an array of objects, you can use CArrayObj class. It's located in the include file <Arrays\ArrayObj.mqh>.

 

Alternative is to send an empty array as a parameter and then fill it with objects:

 

 

Thanks drazen64! 
Reason: