Also, I can solve the problem while still making use of the constants by assigning the constants to variables at the beginning of the function, then using the variables instead of the constants in the rest of the function and it compiles successfully, like the following.
But I'd like to understand why I have to do this. Seems like an error in the compiler? ??
... int histBuy=HIST_BUY; int histSell=HIST_SELL; int histThick=HIST_THICK; int histThin=HIST_THIN; ... for(int itest=1; itest<=TCount-1; itest++) { if(TThickBuffer[itest]<0 && buyOrSell==histSell) consecutiveThickSell++; if(TThickBuffer[itest]>0 && buyOrSell==histSell) break; if(TThickBuffer[itest]>0 && buyOrSell==histBuy) consecutiveThickBuy++; if(TThickBuffer[itest]<0 && buyOrSell==histBuy) break; } ...
Hello,
I have a situation I'm stuck on. The defined constants compile in the else block, but fail to compile in the for block, generating an "unexpected token" error.
If I change the constants in the for block to literal 1 or 2, then the program compiles successfully.
NOTE: the function is called from within the OnCalculate(), if that matters for some reason.
Of course I can change it to literals and make it work for now, but I'd really like to know why one use of the defined constants compiles and another does not, within the same function. Doesn't make sense to me.
Any ideas?
Remove ";" from the #define
Preprocessor replaces constant HIST_BUY with "1;"
When you have ";" after the constant in your code, you get "
buyOrSell=1;;
and it works, but in the comparison statement, after replacement you get
... buyOrSell==1;)
and that does not compile.
Constant value is everything after the constant name, there is no line delimiter. Before compiler, preprocessor simply replaces constants in your code with the values from the #define statement.
Read the documentation
![Documentation on MQL5: Language Basics / Preprocessor / Macro substitution (#define) Documentation on MQL5: Language Basics / Preprocessor / Macro substitution (#define)](https://c.mql5.com/36/55/documentation-on-mql5-language__1.png)
- www.mql5.com
Remove ";" from the #define
Preprocessor replaces constant HIST_BUY with "1;"
When you have ";" after the constant in your code, you get "
and it works, but in the comparison statement, after replacement you get
and that does not compile.
Constant value is everything after the constant name, there is no line delimiter. Before compiler, preprocessor simply replaces constants in your code with the values from the #define statement.
Read the documentation
Good grief. Thank you for helping me de-idiot myself LOL!!!!
Using the preprocessor should be your last choice. | #define HIST_BUY 1; #define HIST_SELL 2; #define HIST_NONE 0; |
Recommend | enum Direction{HIST_NONE, HIST_BUY, HIST_SELL}
|
![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
Hello,
I have a situation I'm stuck on. The defined constants compile in the else block, but fail to compile in the for block, generating an "unexpected token" error.
If I change the constants in the for block to literal 1 or 2, then the program compiles successfully.
NOTE: the function is called from within the OnCalculate(), if that matters for some reason.
Of course I can change it to literals and make it work for now, but I'd really like to know why one use of the defined constants compiles and another does not, within the same function. Doesn't make sense to me.
Any ideas?