I would like to be able to split classes in MQL4 across files, i.e. into the definition in an include/.mqh file and an implementation in a library/.mq4 file. How do we do this - I keep getting compile errors ("'function_name' must have a body")?
E.g. I can take a subset of the code at https://docs.mql4.com/basis/oop/class_templates, and put the definition into a .mqh file:
#import "library.ex4"
//+------------------------------------------------------------------+
//| Class for a free access to an array element |
//+------------------------------------------------------------------+
template<typename T>
class TSafeArray
{
protected:
T m_array[];
public:
//--- operator for accessing the array element by index
T operator[](int index);
};
#import
and put the implementation into a .mq4 file (called library.mq4):
#property library
//+------------------------------------------------------------------+
//| Receiving an element by index |
//+------------------------------------------------------------------+
template<typename T>
T TSafeArray::operator[](int index)
{
static T invalid_value;
//---
int max=ArraySize(m_array)-1;
if(index<0 || index>=ArraySize(m_array))
{
PrintFormat("%s index %d is not in range (0-%d)!",__FUNCTION__,index,max);
return(invalid_value);
}
//---
return(m_array[index]);
}
This was asked previously, but the main answer put both definition and implementation into the .mqh file: What is the correct way to define MQL4 "#import of static class methods"?. Is there any way around this?