Class A reference Class B and the other way round

 

Hello,

I don't know how to make a forward reference between two classes (contained in two different files):

[file A.mqh]

#include "B.mqh"

class A
{
    B* myclassB;
};

[file B.mqh]

#include "A.mqh"

class B
{
    A* myclassA;
};

Clearly this won't compile as the declaration "myclassA" is defined as having no type. Even explicitly including file A.mqh has no efect. Putting both classes in one file is ovbiously the same.

How can i make possible this kind of recursion in MQL5?

Thank you guys.

[EDIT]: I believe this one should go in the GENERAL DISCUSSION, sorry, i read this topic after posting: https://www.mql5.com/en/forum/6775

New Forum Category: Trading Systems
New Forum Category: Trading Systems
  • www.mql5.com
New Forum Category: Trading Systems.
 

class B;

class A
  {
   B *m_b;
  };

class B
  {
   A *m_a;
  };

 
TripleHeinz:

Hello,

I don't know how to make a forward reference between two classes (contained in two different files):

[file A.mqh]

[file B.mqh]

Clearly this won't compile as the declaration "myclassA" is defined as having no type. Even explicitly including file A.mqh has no efect. Putting both classes in one file is ovbiously the same.

How can i make possible this kind of recursion in MQL5?

Thank you guys.

[EDIT]: I believe this one should go in the GENERAL DISCUSSION, sorry, i read this topic after posting: https://www.mql5.com/en/forum/6775

Hi TripleHeinz

Include : https://www.mql5.com/en/docs/basis/preprosessor/include says "The preprocessor replaces the line #include <file_name> with the content of the (include) file ... ".

 

 

Documentation on MQL5: Language Basics / Preprocessor / Including Files (#include)
Documentation on MQL5: Language Basics / Preprocessor / Including Files (#include)
  • www.mql5.com
Language Basics / Preprocessor / Including Files (#include) - Documentation on MQL5
 
Heinz Traub:

Hello,

I don't know how to make a forward reference between two classes (contained in two different files):

[file A.mqh]

[file B.mqh]

Clearly this won't compile as the declaration "myclassA" is defined as having no type. Even explicitly including file A.mqh has no efect. Putting both classes in one file is ovbiously the same.

How can i make possible this kind of recursion in MQL5?

Thank you guys.

[EDIT]: I believe this one should go in the GENERAL DISCUSSION, sorry, i read this topic after posting: https://www.mql5.com/en/forum/6775

Just add the line `class {ClassName};` above the #include  line to make it compile:

[file A.mqh]

class B;
#include "B.mqh"

class A
{
    B* myclassB;
};


[file B.mqh]

class A;
#include "A.mqh"

class B
{
    A* myclassA;
};

So lonG

Daniel


Reason: