CArrayObj and struct

 

Hello,


I'm trying to add a series of structures to a CArrayObj but having problems. Could someone point me to the solution please?


My code is as follows:

#include <Arrays/ArrayObj.mqh>

struct test_t
{
   double   a;
   double   b;
   datetime c;   
};

CArrayObj *obj = new CArrayObj;

void OnStart()
  {
//---
   test_t t;
   t.a = 1.0;
   t.b = 2.0;
   t.c = iTime(NULL,0,0);
   
   obj.Add(t);
   
   const test_t r = obj.At(0);
   
   PrintFormat("obj[0].a = %f", r.a);
    
   delete obj;
  }

and I'm receiving these compilation errors:

Compilation errors

Seems to be a conversion type of problem but I've tried casting to both test_t and CObject with no success. Help please?

Thanks.

 

Have also tried a class rather than a structure and that doesn't work either:

Code:

#include <Arrays/ArrayObj.mqh>

class CTest : protected CObject
{
private:
   double   a;
   double   b;
   datetime c;   
   
public:
   void setA(const double value) { a = value; };
   void setB(const double value) { b = value; };
   void setC(const datetime value) { c = value; };
   double getA() { return a; };
};

CArrayObj *obj = new CArrayObj;

void OnStart()
  {
//---
   CTest tc;
   tc.setA(1.0);
   tc.setB(2.0);
   tc.setC(iTime(NULL,0,0));
   
   obj.Add(tc);
   
   const test_t r = obj.At(0);
   
   PrintFormat("obj[0].a = %f", r.getA());
    
   delete obj;
  }

Errors:

Compilation errors

 

It works only with objects derived from CObject. And that means classes only.

class test_t : public CObject
{
public:
   double   a;
   double   b;
   datetime c;   
};

or

class CTest : public CObject
{
public:
   test_t m_t;
};
 
lippmaje:

It works only with objects derived from CObject. And that means classes only.

or

Ok, thanks very much.

 

This should work (untested):

   CTest tc;
   tc.setA(1.0);
   tc.setB(2.0);
   tc.setC(iTime(NULL,0,0));
   
   obj.Add(&tc);
   
   CTest *r = obj.At(0);
   
   PrintFormat("obj[0].a = %f", r.getA());
Reason: