2

The compiler keeps whining about the need of a constant for the case type in a switch(){...}. But I have provided a freaking constant. - sorry, /rant mode off

Up in my class I have defined the type plus the TYPE_BULLISH and TYPE_BEARISH constants of the int types. Then I assigned values:

static const int TYPE_BULLISH = 0x001;
static const int TYPE_BEARISH = 0x002;

And I assigned the variable type a value of:

type = TYPE_BULLISH;

Then in the constructor

switch(type) {
    case TYPE_BULLISH: Print("Bullish"); break;
    case TYPE_BEARISH: Print("Bearish"); break;
    default:           Print("Doji");
}

Output error:

'TYPE_BULLISH' - constant expression required

Q1: Any idea what is going on here?

I mean,
Q2: I provided a constant, right?

2 Answers 2

3

Try to use #define instead (note: No ; at the end of #define):

#define TYPE_BULLISH    0x001
#define TYPE_BEARISH    0x002
int type = TYPE_BULLISH;

switch(type) {
    case TYPE_BULLISH: Print("Bullish"); break;
    case TYPE_BEARISH: Print("Bearish"); break;
    default:           Print("Doji");
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, is this global or does it apply to the current class only?
@Nique, your issue is a syntactic-error. Your code snippets, as you have described above, ignore MQL4 scope-of-validity. #define just hides that principal misconcept of a proper structured-software design.
@Nique: It is Global-scoped. It is quite commonly used. You can see #define being used by many of the built-in MT4 sample/utilities. For example: stderror.mqh, stdliberr.mqh, winuser32.mqh, etc. #define is being used all the time.
1

A2: No, you have not provided a constant.

A1: Your MQL4 code is syntactically wrong and cannot get compiled.


While @JosephLee has provided a error-avoiding substitute, the problem is hidden in a way, MQL4-code retains a scope-of-validity

Your class-level designations cease to exist outside the container, where these have been declared / initialised.

Your code, outside of such container ( typically the inner-most surrounding { ... }-code block / hierarchy-level, where such a declaration took place ) simply cannot get compiled, as the compiler obeys the rules of scope-of-validity during the source-code parsing.

Q.E.D.

One may use "globally"-visible variables in case of a need for such centrally managed predefined constants.

Beware, that #include, #import, #property library and #export introduce more havoc into this circus.

You were warned, at least...


A use of a utility-function:

enter image description here


Example code how to use it from one library project:

enter image description here

4 Comments

So basically, the Switch() case is useless unless you hardcode the to be matches values over and over again all over the place? Seems weird :).
@Nique btw, if you need not some artificial magic with a fixed memory location data-mapper, bypassing a stack-allocation mechanics, for some pretty nasty things, a static const int ... has no reason for storing a value. ( #define is a pre-compiler parsing directive, so actually no scope-checking takes place, just a lexer sequential code-stream analysis, so be carefull just about overlapping values & a global Project unique-error-number catalogue maintenance as in example -- #define anotherErrorNUMBER HAUSNUMERO + 161 ( a trick seen from Pieter Hintjens' code foundry :o) ).
Hmm #define is not the way to go i think. I want to keep the stuff inside the class scope. No need for global.
@Nique just for clarity, as you 've asked in first comment, #define <aLiteralSymbol> <aValue> are pre-processor directives and are thus lexically interpreted for the whole rest of the source-code file, ignoring any code-unit hierarchy & encapsulation, this a simple #include ... may help you in the very same manner as the .h header files do in C.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.