Search code examples
mql4

Mql4 undeclared identifier for delete object


I am using the code from the example here Keep order details in arrays?

I am getting an error of listOfTrades - undeclared identifier when I add the code in the

void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();

   delete(listOfTrades);

  }

I have a feeling it is because listOfTrades is declared in the OnInit() and not outside global but not sure how that would affect it.

Main.mql

#include <CTrade.mqh>

int OnInit()
  {
    CArrayObj *listOfTrades=new CArrayObj;
  }

void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();

   delete(listOfTrades);

  }

Solution

  • Yes, you have to declare that globally, before calling OnInit(). Missed that point in the mentioned question, will update.

    If you pass the object by pointer somehow void OnTick(){ doSomeFunction(listOfTrades); } then you should make sure the same variable name is not used (at least in the same file), otherwise you will have warnings that some day may become a bug.

    void doSomeFunction(CArrayObj* _listOfTrades)// not "listOfTrades" !!! 
      {
       for(int i=_listOfTrades.Total()-1;i>=0;i--)
         {
          CTrade* trade=_listOfTrades.At(i);
          // action with CTrade object
         }
      }