Errors, bugs, questions - page 2362

 
Ilya Malev:

I don't have functions there, but it's exactly the task you described. I have a CNode ( which has methods Prev(), Next(), etc.) as the base class of the list, and the loop calls the fields of COrder, which is its descendant. The loop calls fields of COrder, which is its descendant. The loop is declared in defines 1 time and then is used everywhere.

I tried to follow your example, but I want to use typedef

#property strict
#include <Object.mqh>
#include <Arrays\List.mqh>
//+------------------------------------------------------------------+
class CMyclass:public CObject
  {
public:
   int               x;
   double            y;
   void              CMyclass(void):x(-1),y(-2.2) { }   
  };
  
typedef CMyclass (*PtrCMyclass)(CObject*); 
PtrCMyclass Myclass;
//+------------------------------------------------------------------+
void OnStart()
  {
   CList *base=new CList;
   for(int i=0;i<3;i++)
     {
      base.Add(new CMyclass);
      Myclass(base.GetCurrentNode()).x = 99;
      Myclass(base.GetCurrentNode()).y = -555.5;
     }
   for(int i=0;i<3;i++)
     {
      Print(Myclass(base.GetNodeAtIndex(i)).x," : ",Myclass(base.GetNodeAtIndex(i)).y);
     }
   delete base;
  }
//+------------------------------------------------------------------+

Now the compiler skips my example, but I need ... something is needed ... but what? I get an error when executing:

nvalid function pointer call in 'tst_file.mq4' (30,7)


 
Igor Makanu:

You're probably right, I tried your example, but I want to use typedef

Now the compiler skips my example, but I need... I need something... but what?

nvalid function pointer call in 'tst_file.mq4' (30,7)


So, I need to write a function and assign it to the pointer)))

typedef CMyclass* (*PtrCMyclass)(CObject*);
CMyclass* FMyCast1(CObject*p){ CMyclass *r = dynamic_cast<CMyclass*>(p); if(CheckPointer( r ) != POINTER_INVALID) return r; else return NULL; }
PtrCMyclass Myclass = FMyCast1;
 
Ilya Malev:

So, you need to write a function and assign it to a pointer)))

Hm, it makes sense, but then it doesn't give me a point to address the class fields if I write the function in the following way

PtrCMyclass Myclass(CObject* obj){ return(GetPointer(obj)); };

'x' - struct or class type expected tst_file.mq4 30 38


 
Try as I wrote above. Although typedef is not really needed here, because it is an unnecessary "padding" between the code and the function call. You still can't overload this call for another class using the same pointer. You will have to make a new one for each class. It's about time you realized that typedefs are little less than completely useless, imho :)
 
Igor Makanu:

((CMyclass*)base.GetNodeAtIndex(i)).x

 
Ilya Malev:
Try as I wrote above. Although typedef is not needed here because it is an extra "spacer" between the code and the function call. You still won't be able to overload this call for another class using the same pointer. You will have to make a new one for each class. It's about time you realized that typedefs are little less than completely useless, imho :)

I tried, but I still don't know how to work with defans, science is complicated , I got an error: tst_file EURUSD,H1: invalid pointer access in 'tst_file.mq4' (31,56)

#property strict
#include <Object.mqh>
#include <Arrays\List.mqh>
//+------------------------------------------------------------------+
class CMyclass:public CObject
  {
public:
   int               x;
   double            y;
   void              CMyclass(void):x(-1),y(-2.2) { }
  };
//+------------------------------------------------------------------+
#define  alive(node)  (CheckPointer(node)==POINTER_DYNAMIC)
#define  loopf(type, node)  for(type *o; o=node;)
//+------------------------------------------------------------------+
void OnStart()
  {
   CList *base=new CList;
   CMyclass *res;
   for(int i=0;i<3;i++)
     {
      base.Add(new CMyclass);
      loopf(CMyclass,base.GetCurrentNode()){ o.x = 99; res.y = 555;}
     }

   for(int i=0;i<3;i++)
     {
      res=base.GetNodeAtIndex(i);
      Print(res.x," : ",res.y);
     }
   delete base;
  }
//+------------------------------------------------------------------+
 
TheXpert:

((CMyclass*)base.GetNodeAtIndex(i)).x

HOORAY! IT WORKED!!!

Thank you!

#property strict
#include <Object.mqh>
#include <Arrays\List.mqh>
//+------------------------------------------------------------------+
class CMyclass:public CObject
  {
public:
   int               x;
   double            y;
   void              CMyclass(void):x(-1),y(-2.2) { }
  };
//+------------------------------------------------------------------+
void OnStart()
  {
   CList *base=new CList;
   for(int i=0;i<3;i++)
     {
      base.Add(new CMyclass);
      ((CMyclass*)base.GetCurrentNode()).x = 99;
      ((CMyclass*)base.GetCurrentNode()).y = 555;
     }

   for(int i=0;i<3;i++)
     {
      Print(((CMyclass*)base.GetNodeAtIndex(i)).x," : ",((CMyclass*)base.GetNodeAtIndex(i)).y);
     }
   delete base;
  }
//+------------------------------------------------------------------+

;)

 
Igor Makanu:

I tried it, but I still don't know how to work with defans, science is complicated , I got error: tst_file EURUSD,H1: invalid pointer access in 'tst_file.mq4' (31,56)

I'd be very surprised if my loop would work with your library node)))

I was referring to the typedef code in my last post, of course)))

Igor Makanu:

HOORAY! IT WORKED!!!

Thanks!

;)

Really, why didn't you try the most obvious one at once? :) You wanted to rock out with the typedef so badly? =)))

 
Ilya Malev:

Really, why didn't you try the most obvious one first? :) You wanted to rock with typedef so badly? =)))

I tried it! I was sure that I wouldn't be able to dereference a pointer using MQL, but it turned out to be trivial - I should have put more brackets! ))))

SZY: typedef is a topic, I tried it, but it didn't go well either. I still need to figure out where I messed up, my example should work with typedef too!

 
Alexandr Sokolov:

What is the reason why the indicator may not work only on the M1 chart, but on all other charts it works correctly? (MQL5). Here is the function - and when I give it 0 on the M1 chart, it somehow does not sum up one variable


This means that there is a candlestick with Open = Close, while the code is written incorrectly and does not contain the check of division by 0. This unfortunate fact does not depend on the TF.

Reason: