Warning!

 


This warning is really annoying, I don't know why the developer team won't fix it. This warning is nonsense when a global variables name is the same as a local variables name in another file. The compiler should generate the warning only when both declarations are in the same file.

When we use a lot of libraries it gets even worse.

 
Ehsan Tarakemeh:


This warning is really annoying, I don't know why the developer team won't fix it. This warning is nonsense when a global variables name is the same as a local variables name in another file. The compiler should generate the warning only when both declarations are in the same file.

When we use a lot of libraries it gets even worse.

don't blame MQL guys...blame B. Stroustrup but i don't think he will listen anyway

 
Ehsan Tarakemeh:


This warning is really annoying, I don't know why the developer team won't fix it. This warning is nonsense when a global variables name is the same as a local variables name in another file. The compiler should generate the warning only when both declarations are in the same file.

When we use a lot of libraries it gets even worse.

You don't understand the the mqh-files. They are inserted and then the compilers takes both as one file

 
Teboho Rakotsoane:

I think if you declare it once in a local scope you should not be getting that. Sometimes declaring it twice causes the problem rather change the naming. 

Suppose you write a library for me with thousands of files, functions and classes. You write a function with a local variable "temp". I include your library to my main code and declare a global variable with the name "temp" and the compiler says hey warning, your friends variable hides your variable. Then I try to change the name to temp1 and it says the same warning again and again.

The problem with the compiler is that it dose not understand the file scope. It mixes everything then compiles. 

 
Ehsan Tarakemeh:

Suppose you write a library for me with thousands of files, functions and classes. You write a function with a local variable "temp". I include your library to my main code and declare a global variable with the name "temp" and the compiler says hey warning, your friends variable hides your variable. Then I try to change the name to temp1 and it says the same warning again and again.

The problem with the compiler is that it dose not understand the file scope. It mixes everything then compiles. 

you could wrap the library inside a namespace though
 
Ehsan Tarakemeh:

Suppose you write a library for me with thousands of files, functions and classes. You write a function with a local variable "temp". I include your library to my main code and declare a global variable with the name "temp" and the compiler says hey warning, your friends variable hides your variable. Then I try to change the name to temp1 and it says the same warning again and again.

The problem with the compiler is that it dose not understand the file scope. It mixes everything then compiles. 

Just try to avoid to use global variables or mark them in a special way.

 

This warning warns you about a situation that may cause really nasty and hard to find bugs.

Changing naming must work, you are just doing something wrong.

 
Teboho Rakotsoane:

I understand. Its really annoying then cause whats the alternative way if even changing naming does not work? 

The compiler should respect the scope of files.

 
roshjardine:
you could wrap the library inside a namespace though
It's not my library. It's from MetaQoute, and it's not a good practice to edit other's libraries.

The scope of a file should works like namespace by itself.


 
nicholish en:

In python yes, but not in C langs and not in MQL. This is a must read for you...  https://www.learncpp.com/cpp-tutorial/why-global-variables-are-evil/

I'm programming for 20 years by now and I know how to use global variables the right way.

No, It's absolutely not the case for C languages. Accessing a not defined/declared variable in C languages is an error, but in MQL it's not.

 
MQLn is not C and it is not C++. Adjust to it.
Reason: