Download MetaTrader 5

compilation error when declaring a variable inside a switch statement

To add comments, please log in or register
Your broker does not support mobile MetaTrader 5? Ask him to buy them!
Mihai Ionescu
1300
Mihai Ionescu 2010.05.22 12:26 

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 2010.05.22 18:42  
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
1300
Mihai Ionescu 2010.05.22 19:53  
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
6606
Alexey Da 2010.07.29 13:44  

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.

Slawa
Moderator
6841
Slawa 2010.07.29 15:01  
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
1300
Mihai Ionescu 2010.08.01 21:52  
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? 

Andrei Moraru
1160
Andrei Moraru 2010.08.02 12:35  
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
11826
Rashid Umarov 2010.08.02 18:35  
Mihai Ionescu
1300
Mihai Ionescu 2010.08.03 22:03  
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
1300
Mihai Ionescu 2010.08.03 22:05  
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.
Andrei Moraru
1160
Andrei Moraru 2010.08.04 11:07  
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