- Generating included code - Developing programs
- MetaEditor environment folders
- Including Files (#include)
I would like to involve OnInit() in a .mqh file so that OnInit() will be executed both in the main .ex4 file and in the .mqh file. Is this possible? One must then override OnInit() in the .mqh file in some way.
The #include directive literally copies and pastes all text from the mqh file to that exact place in the file prior to compilation. In other words, it's not like other languages which makes you define the module namespace. So if you include a mqh that has defined an OnInit with a file that already has one then you will get errors because now you are redefining a function with the same signature.
The #include directive literally copies and pastes all text from the mqh file to that exact place in the file prior to compilation. In other words, it's not like other languages which makes you define the module namespace. So if you include a mqh that has defined an OnInit with a file that already has one then you will get errors because now you are redefining a function with the same signature.
yep, forgot to mention that I already got the error and therefor was thinking of overriding OnInit() in the .mqh file. I am sure that this is possible but the suggestion from whroeder seems more straighforwarded so I will stick to that.
Thanks a lot anyway for the explanation :-)
yep, forgot to mention that I already got the error and therefor was thinking of overriding OnInit() in the .mqh file. I am sure that this is possible but the suggestion from whroeder seems more straighforwarded so I will stick to that.
Thanks a lot anyway for the explanation :-)
You can't override a function like that... you could overload it with a different signature, but remember -- #include grabs the text from the other file and pastes it as the exact location that you are calling it from
So
//other.mqh int OnInit(){ return INIT_FAILED; } double global_double = 1.21;
//program.mq4 int OnInit() {return INIT_SUCCEEDED;} #include "other.mqh"
This is what the compiler sees
//program.mq4 int OnInit() {return INIT_SUCCEEDED;} int OnInit(){ return INIT_FAILED; } double global_double = 1.21;
Which is an error because you can't redefine a function with the same signature.
I accidentally deleted my last post instead of editing... You can make a wrapper similar to a decorator in Python like this.
init_decorator.mqh
int OnInit() { Print("Enter decorator"); int main_file_init = __OnInit(); Print("Exit decorator"); return main_file_init; } #ifndef OnInit #define OnInit __OnInit #endif
main.mq4
#include "init_decorator.mqh" int OnInit() { Print("I am the OnInit func in the main file."); return(INIT_SUCCEEDED); }
output:
Generic EURUSD,H1: Enter decorator
Generic EURUSD,H1: I am the OnInit func in the main file.
Generic EURUSD,H1: Exit decorator
Generic EURUSD,H1: initialized
I accidentally deleted my last post instead of editing... You can make a wrapper similar to a decorator in Python like this.
init_decorator.mqh
main.mq4
output:
I accidentally deleted my last post instead of editing... You can make a wrapper similar to a decorator in Python like this.
init_decorator.mqh
main.mq4
output:
... but...
Error in main: __OnInit Function already defined and has body
Write an OnInitIncludefileName() and call it in OnInit()
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use