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;
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
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.
?!?!??!?!!?!?!
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).
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?
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.
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.
- www.mql5.com
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.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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...