inconsistent "unexpected token" error when compiling indicator

Christian Berrigan
127

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?


// from the top of the file in the properties area....
#define HIST_BUY 1;
#define HIST_SELL 2;
#define HIST_NONE 0;

//<snip>...

//+------------------------------------------------------------------+
//| Custom indicator function                                        |
//+------------------------------------------------------------------+
// function is called from OnCalculate()
int getHistCount(double &TThickBuffer[], double &TThinBuffer[], int THandle, int &buyOrSell, int &thickOrThin, ENUM_TIMEFRAMES TPeriod)
   {

//<snip>...

      else if(TThickBuffer[1]>0) 
         {
         buyOrSell=HIST_BUY;
         thickOrThin=HIST_THIN;
         }
      for(int itest=1; itest<=TCount-1; itest++)
         {
         if(TThickBuffer[itest]<0 && buyOrSell==HIST_SELL) consecutiveThickSell++;
         if(TThickBuffer[itest]>0 && buyOrSell==HIST_SELL) break;
         if(TThickBuffer[itest]>0 && buyOrSell==HIST_BUY) consecutiveThickBuy++;
         if(TThickBuffer[itest]<0 && buyOrSell==HIST_BUY) break;
         }

// code continues...
Christian Berrigan
127
Christian Berrigan  

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;
         }
...
Drazen Penic
3309
Drazen Penic  
Christian Berrigan:

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)
  • www.mql5.com
Macro substitution (#define) - Preprocessor - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Christian Berrigan
127
Christian Berrigan  
Drazen Penic #:

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!!!!

William Roeder
27833
William Roeder  
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}