structure have objects and cannot be copied

 

Hi

The below code gives the error "structure have objects and cannot be copied"

any suggestions on how to get it working?


  class Line {
   public:
      string name;
      double value;
  }; 
  
int OnInit() { 
   Line lines[];
   proccessAllLines(lines);
   
   return(INIT_SUCCEEDED); 
}


void proccessAllLines(Line& lines[]){

   Line line;
   line.name = "line1";
   line.value = 55.5;
   arrayPush(lines, line);
 
}
template<typename T>
uint arrayPush(T &arr[], T &val){       // val worked with simple type, changed to &val to work with complex types also :(
   uint oldsize = ArraySize(arr);
   ArrayResize(arr,oldsize+1);
   arr[oldsize] = val;   // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< line giving the error
      
   return ArraySize(arr);
}
 

So what you're doing is creating an array of static objects and then trying to copy the values from from one object to another. You actually need to be working with pointers and pointer arrays. Furthermore you should also ensure that you have implemented proper memory management. Thankfully, MQL has some pointer collections built into the standard library that automatically handle memory and size. Just make sure you're always inheriting from CObject and that you're using the pointers to dynamic objects. Here is an example.


#include<Arrays\List.mqh>
class Line : public CObject
{
  public:
    string name;
    double value;
};

CList line_list;

void void OnStart()
{
    for(int i=10; i > 0; i--)
    {
        Line *line = new Line
        line.name = "line_"+string(i);
        line.value = i;
        line_list.Add(line);
    }

    Line *line = line_list.GetFirstNode();
    for(; CheckPointer(line); line=line.Next())
        Print(line.name);
}


https://www.mql5.com/en/docs/standardlibrary/datastructures

Documentation on MQL5: Standard Library / Data Collections
Documentation on MQL5: Standard Library / Data Collections
  • www.mql5.com
This section contains the technical details on working with various data structures (arrays, linked lists, etc.) and description of the relevant components of the MQL5 Standard Library.
Reason: