How can i add an object to CList in my expert advisor?

Dear all,

I would like to store object instances into a CList object but I’m always getting an error telling me “parameter conversion not allowed”…

What I would like to do is to create a list called “virtualPositions”. And into this list I want to store object instances of type “CVirtualPosition”.

Now, when I’m trying to add such an object instance of CVirtualPosition to this list (using method “Add”), I’m getting the error “parameter conversion not allowed”.

See extract of my code below:

CList *virtualPositions = new CList();

CVirtualPosition *vp = new CVirtualPosition(currentTime, openPrices[0], comment);


Any idea upon what’s going wrong here?

Thanks in advance!!

I think I solved it by myself… I need to make sure that my class “CVirtualPosition” inherits from CObject…

class CVirtualPosition : public CObject
      datetime openTime;
      double openPrice;
      string comment;
      CVirtualPosition(datetime t, double p, string c) 
         openTime = t; 
         openPrice = p; 
         comment = c;
      datetime            GetOpenTime()     const  {return(openTime);     }
      double              GetOpenPrice()    const  {return(openPrice);    }
      string              GetComment()      const  {return(comment);      } 

Well done. And it’s great that you posted your solution for others who may have a similar problem in the future.

I would also like to suggest that you instantiate your CList object as a statically allocated object so whenever its scope runs out it will automatically be deleted as well as all of the dynamically allocated memory of the objects that it holds. It’s ease to forget to use delete which will cause memory leaks.

   CList list;

   for(int i=0; i<10; i++)
      list.Add(new CObject());
//list automatically deleted here (scope)
//dynamic objects in list also deleted (FreeMode == true)

Just out of curiosity, is your background in Java?

Yes indeed, my background lies at Java… But in the past, I’ve been working with C/C++ too but that’s already a long time ago.

Great tip about statically allocating the CList object. Indeed now for the time being, I did make use of the “delete” function in e.g. the OnDeinit() and after removing objects from this list…

Just remember that CList and (the more efficient) CArrayObj automatically destroy all dynamic objects held in their collection upon the call to the deconstructor <by default>. This is important to know because you will eventually run into and issue when you pass objects from one collection to another and forget to set the FreeMode to false… You end up with a collection with invalid pointers because one automatically deleted the objects the other was pointing to.

Thanks for the tip!!