Download MetaTrader 5

compilation error when declaring a variable inside a switch statement

To add comments, please log in or register
Mihai Ionescu
1505
Mihai Ionescu  

Creating a new custom indicator and having this switch statement:

switch (rates_total) {
   case 1:
         int r = 5;
         if (r==rates_total) printf("bla bla");
      break;
   case 2:
         printf("bla bla 2");
      break;      
  }

will result the following compilation error:

'int' - initialization of variable skipped by 'case' label, use { } blah.mq5 49 10

-----------

declaring variable outside switch statement it's ok, inside it's not...

bug? I guess it's not a feature... 

Step on New Rails: Custom Indicators in MQL5
  • 2009.11.23
  • Андрей
  • www.mql5.com
I will not list all of the new possibilities and features of the new terminal and language. They are numerous, and some novelties are worth the discussion in a separate article. Also there is no code here, written with object-oriented programming, it is a too serous topic to be simply mentioned in a context as additional advantages for developers. In this article we will consider the indicators, their structure, drawing, types and their programming details, as compared to MQL4. I hope that this article will be useful both for beginners and experienced developers, maybe some of them will find something new.
Paul
784
Paul  
ifmihai posted  :

Creating a new custom indicator and having this switch statement:

will result the following compilation error:

'int' - initialization of variable skipped by 'case' label, use { } blah.mq5 49 10

-----------

declaring variable outside switch statement it's ok, inside it's not...

bug? I guess it's not a feature... 

The compilation error is surprisingly helpful: you need to put the offending declaration inside a { } block.  From memory this is probably standard C++ syntax

   switch(rates_total) 
     {
      case 1:
        {
         int r=5;
         if(r==rates_total) printf("bla bla");
        }
      break;

 

 

 

Mihai Ionescu
1505
Mihai Ionescu  
phampton :

The compilation error is surprisingly helpful: you need to put the offending declaration inside a { } block.  From memory this is probably standard C++ syntax

 

 

 

?!?!??!?!!?!?!

So what's the meaning of variable scope then?

It's the first time I see such thing...

It's just not right, whatever c++ syntax is...

 It's like you can declare variable in a for statement but not in while statement ... that's silly 

Alexey Da
Moderator
6601
Alexey Da  

Did you try this code in C++?

The error which was returned by compiler, contain advice to avoid your problem. Just follow this advice. It helps you to set exact borders of variable scope.

Slava
Moderator
6961
Slava  
ifmihai:

?!?!??!?!!?!?!

So what's the meaning of variable scope then?

It's the first time I see such thing...

It's just not right, whatever c++ syntax is...

 It's like you can declare variable in a for statement but not in while statement ... that's silly 

For example this code

switch(var)
  {
   case 1 :
      int var1=5;
      Print(var1);
      break;
   case 2 :
      Print(var1);
      break;
  }

What value should be printed if var==2?!?!?

Undefined because var1 is uninitialized (but formally declared in the same scope).

 

 

Mihai Ionescu
1505
Mihai Ionescu  
stringo:

For example this code

What value should be printed if var==2?!?!?

Undefined because var1 is uninitialized (but formally declared in the same scope).

 

 

The problem was (and probably still is, I didn't check with last build) that compiler doesn't let me declare the var1 variable like you did.

Anyway, I don't want to declare var1 in case 1, and use it in case 2.  I just want to declare locally a variable in a case statement. 

Instead, to avoid "tree optimization error" problem, I declared some 30 variables outside switch, to avoid problems. 

And from what you say, the scope in a switch statement is vague. Are you saying the scope of var1 is all switch statement? Isn't it just the case? 

Andriy Moraru
1260
Andriy Moraru  
ifmihai:

The problem was (and probably still is, I didn't check with last build) that compiler doesn't let me declare the var1 variable like you did.

Anyway, I don't want to declare var1 in case 1, and use it in case 2.  I just want to declare locally a variable in a case statement. 

Instead, to avoid "tree optimization error" problem, I declared some 30 variables outside switch, to avoid problems. 

And from what you say, the scope in a switch statement is vague. Are you saying the scope of var1 is all switch statement? Isn't it just the case? 

What's the difficulty in using the brackets { }, like phampton showed you? It will do exactly what you want. And yes, variable scope is defined by the brackets.
Rashid Umarov
Admin
12399
Rashid Umarov  
Mihai Ionescu
1505
Mihai Ionescu  
enivid:
What's the difficulty in using the brackets { }, like phampton showed you? It will do exactly what you want. And yes, variable scope is defined by the brackets.

read the thread with tree optimization error then: https://www.mql5.com/en/forum/1524/unread#unread

You made the presumtion I don't / didn't want to use brackets.

The problem is that it generates "tree optimization error" error. But the trick is that it doesn't appear all the time, but here and there, apparently with no reason.  

"Tree optimization error" error
  • www.mql5.com
I don't know if it's my indicator or not, but the behavior is really strange, so I presume it's a bug.
Mihai Ionescu
1505
Mihai Ionescu  
the same response goes to this reply also. The problem is not bracketing. The problem is with brackets inside case's of a switch statement.
Andriy Moraru
1260
Andriy Moraru  
ifmihai:

read the thread with tree optimization error then: https://www.mql5.com/en/forum/1524/unread#unread

You made the presumtion I don't / didn't want to use brackets.

The problem is that it generates "tree optimization error" error. But the trick is that it doesn't appear all the time, but here and there, apparently with no reason.  

Sorry. Then you'll just have to wait for the bug-fix for this problem.
12
To add comments, please log in or register