0

我正在尝试在应用程序中使用CList Search方法。我在下面附上了一个非常简单的例子。在这个例子中,我总是在变量中得到一个空指针result。我在 MQL4 和 MQL5 中尝试过。有没有人使搜索方法工作?如果是这样,我的错误在哪里?对于我的问题,我指的是 MQL 中链表的这种实现(它是标准实现)。当然,在我的应用程序中,我不想找到第一个列表项,而是要找到符合特定条件的项。但即使是这个微不足道的例子也不适合我。

#property strict
#include <Arrays\List.mqh>
#include <Object.mqh>

class MyType : public CObject {
   private:
      int val;
   public:
      MyType(int val);
      int GetVal(void);   
};
MyType::MyType(int val): val(val) {}
int MyType::GetVal(void) {
   return val;
}

void OnStart() {
   CList *list = new CList();
   list.Add(new MyType(3));

   // This returns a valid pointer with
   // the correct value
   MyType* first = list.GetFirstNode();

   // This always returns NULL, even though the list
   // contains its first element
   MyType* result = list.Search(first);

   delete list;
}
4

1 回答 1

0

CList是一种链表。一个经典的数组列表CArrayObj在 MQL4/5 中使用Search()和其他一些方法。virtual int Compare(const CObject *node,const int mode=0) const在调用搜索之前,您必须对列表进行排序(因此实现方法)。

virtual int       MyType::Compare(const CObject *node,const int mode=0) const {
  MyType *another=(MyType*)node;
  return this.val-another.GetVal();
}

void OnStart(){
  CArrayObj list=new CArrayObj();
  list.Add(new MyType(3));
  list.Add(new MyType(4));
  list.Sort();

  MyType *obj3=new MyType(3), *obj2=new MyType(2);
  int index3=list.Search(obj3);//found, 0
  int index2=list.Search(obj2);//not found, -1
  delete(obj3);
  delete(obj2);
}
于 2019-09-05T07:31:57.510 回答