I've 4 classes across 4 different files in the following hierarchy:
|-- Terminal (Chart)
| |-- Chart (Draw, Market)
| | |-- Draw
| | |-- Market
In the brackets are class pointers which class implements as class variables.
So Draw and Market classes extends Chart, which extends Terminal. Although I still want to define some class pointer, so Terminal class it-self has chart
pointer to Chart class.
The files are:
Terminal.mqh
// Forward declaration.
class Chart;
class Terminal;
// Includes.
#include "Chart.mqh"
class Terminal {
protected:
Chart *chart;
};
Chart.mqh
// Forward declaration.
class Chart;
class Terminal;
// Includes.
#include "Terminal.mqh"
class Chart : public Terminal {
protected:
// Includes.
#include "Draw.mqh"
#include "Market.mqh"
// Class variables.
Draw *draw;
Market *market;
};
Note: The includes to Draw
and Market
files are placed after class definition just to make sure that the current Chart
class is being defined at the time when these includes are loaded, and just before declaration of variables which are using it.
Draw.mqh
// Forward declaration.
class Chart;
class Draw;
#include "Chart.mqh"
class Draw : public Chart {
// Some drawing methods.
};
Market.mqh
// Forward declaration.
class Chart;
class Market;
// Includes.
#include "Chart.mqh"
class Market : public Chart {
// Some market methods.
};
To avoid any compiler errors while the includes are loaded, I'm making forward declaration, however the compilation still fails.
The errors are as follow (depending which file I'm trying to compile):
Terminal.mqh
(3 error(s), 0 warning(s))
'Terminal' - struct undefined Chart.mqh 8 22
'Chart' - struct undefined Draw.mqh 7 21
'Chart' - struct undefined Market.mqh 8 23
Chart.mqh
(2 error(s), 0 warning(s))
'Chart' - struct undefined Draw.mqh 7 21
'Chart' - struct undefined Market.mqh 8 23
Draw.mqh
(1 error(s), 0 warning(s))
'Chart' - struct undefined Market.mqh 8 23
Market.mqh
(1 error(s), 0 warning(s))
'Chart' - struct undefined Draw.mqh 7 21
Tested in the latest build 1498.
The above errors are the same for both MQL4 and MQL5 compiler builds.
Is there any solution to above problem? I am missing anything?
To clarify, my goal is to keep each class in a separate file.
It is not possible to inherit a class from forward declaration.
So the files needs to be included after the class has been defined.
The following structure has been suggested to me:
Terminal.mqh
// Forward declaration.
class Chart;
class Terminal;
class Terminal {
protected:
Chart *chart;
};
// Includes.
#include "Chart.mqh"
// Terminal methods implementation
Chart.mqh
class Chart;
class Terminal;
// Includes.
#include "Terminal.mqh"
class Chart: public Terminal
{
protected:
Draw *draw;
Market *market;
public:
void Test()
{
draw.Test();
market.Test();
}
};
#include "Draw.mqh"
#include "Market.mqh"
Draw.mqh
#include "Chart.mqh"
class Draw : public Chart
{
public:
void Test()
{
this.Test();
}
};
Market.mqh
#include "Chart.mqh"
class Market : public Chart
{
public:
void Test()
{
this.Test();
}
};
Still not ideal, but at least it compiles.