MQL4/5 CList 搜索方法始终返回空指针



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

#property strict
#include <ArraysList.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;
}
CList

是一种链表。经典数组列表在 MQL4/5 中使用Search()和其他一些方法CArrayObj。在调用搜索之前,您必须对列表进行排序(因此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);
}

最新更新