Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structure have objects and cannot be copied

I'm trying to start with my first MQL4 expert advisor,

I've created a struct to handle my orders:

struct Order
  {
   int               pair;
   int               command;
   double            quantity;
   double            entry;
   double            stopLoss;
   double            profit;
   int               slippage;
   string            comment;
   int               magicNumber;
   datetime          expire;
  };

but it seems I can't do this:

  Order a;
  Order b=a;

the compiler hangs saying:

'=' - structure have objects and cannot be copied

How can I assign a struct?

like image 838
Don Giulio Avatar asked Jun 11 '26 22:06

Don Giulio


1 Answers


My Recommended Answer

You can use classes with pointers instead of structures, which cannot have pointers and can't copy with strings inside,

Examples are below, http://docs.mql4.com/basis/types/object_pointers

Read this to understand classes vs. structures http://docs.mql4.com/basis/types/classes


Alternative Answer with char arrays ( But simple change to this )

Define char array with a fixed size inside a structure instead of a string.

Can use
CharArrayToString( ... )
and
StringToCharArray( str, array, 0, StringLen( str ) )
to work with strings and char arrays


Example:

struct Order
  {
   int               pair;
   int               command;
   double            quantity;
   double            entry;
   double            stopLoss;
   double            profit;
   int               slippage;
   char              comment[10];
   int               magicNumber;
   datetime          expire;
  };

  Order  a;
  string str = "testing\n";

  StringToCharArray( str, a.comment, 0, StringLen( str ) );

  Order b = a;

  Comment( "Array " + CharArrayToString( b.comment ) );
like image 187
PraAnj Avatar answered Jun 15 '26 12:06

PraAnj