Hi!
I can't understand how to define default values for functions in my library. Default values tend to be ignored and I get "wrong parameters count" error message.
Here is my example:
I created simple test library experts\libraries\test.mq4:
void test(int i = 0)
{
}
Then I created .mqh file experts\include\test.mqh:
#import "test.ex4"
void test(int i = 0);
#import
Now I create simple expert experts\simpletest.mq4:
Hi dleonov!
Perhaps your problem is the fact that your 'experts\libraries\test.mq4' does not have a main(start). when you compiled you got no message indicating this? And I beleive you cannot pass an argument to main(). I might be wrong, but I may be right...
WHat you could do is to:
1. forget about import from expert\libraries
2. You have a sintax problem in the include\test.mqh; remove the # import lines and replace the semicolon with{} to give it a body
3. put the include test.mqh in your simpletest.mq4 and recompile. this should work.
Let me know if this helps...
#include <test.mqh>
int start()
{
test();
return(0);
}
And get the following error for test() function call: ')' - wrong parameters count
If I change this function call to test(0), everything compiles, but I have default value for first parameter in .mqh file, like this: void test(int i = 0);
Why it doesn't use default value?
I search google for any clue, but can't find any references about this problem. Anybody knows?
Thanks!
2. You have a sintax problem in the include\test.mqh; remove the # import lines and replace the semicolon with{} to give it a body
He does not have a syntax problem. This is the correct syntax for importing functions.
There seems to be a problem that mql4 is simply not capable of specifying default values for imported functions, if the manual does not mention this then the manual is incomplete. This restriction would be completely counterintuitive and should be separately mentioned in the manual (I did not actually look in the manual now out of laziness, it might indeed be mentioned there somewhere).
What I usually do is not using ex4 libraries at all (for my purposes it is enough to simply (ab)use the header files themselves to put all my "library" functions there along with their implementations). This has the disadvantage of having to recompile all my EAs if the function implementations in the include file change but it has the advantage that the end result is one statically linked .ex4 binary that has no dependencies and can be easily distributed. Since my main include files with my commonly used functions have matured over time and are changed only very seldom nowadays and I have only a handful of EAs in active usage the disadvantages almost completely go away.
And here it is:
https://docs.mql4.com/basis/preprosessor/import
In broken English it states the following:
"Since the imported functions are out of the module to be compiled, the compiler cannot check correctness of parameters passed. This is why, to avoid runtime errors, it is necessary to declare the compliance of types and order of parameters precisely. The parameters passed to imported functions (both from EX4 and from DLL modules) cannot have values by default."
It is not understandable why the compiler does not throw a meaningful error message already at the line where the faulty import is declared.
Ok, I see now. RTFM!
Thank you!
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Question to MQL experts.
I can't understand how to define default values for functions in my library. Default values tend to be ignored and I get "wrong parameters count" error message.
Here is my example. I created simple test library "experts\libraries\test.mq4":
{
}
Then I created .mqh file "experts\include\test.mqh":
void test(int i = 0); // Note the default value for "i"
#import
Now I create simple expert "experts\simpletest.mq4":
int start()
{
// Should be able to call test() function without providing any arguments,
// because it has default value.
// If I change this line to test(0), everything compiles correctly
test(); // Causes "wrong parameters count" compilation error
return(0);
}
And I get the following error for test() function call: ')' - wrong parameters count
If I change this function call to test(0), everything compiles, but I should be able to call test() function without providing any parameters, because I have default value for first parameter in .mqh file, like this: void test(int i = 0);Why it doesn't use the default value?
I search google for any clue, but can't find any references about this problem. Anybody knows?
Thanks!