Your understanding of global variables is correct.
Your error.
Your problem is not the global variables. As other users have already pointed out, your main
function is incorrect; is incorrect because must return int
. The reason for this obligation is because (as it says the language standard C ++ ) otherwise your program will be incorrect, I show it to you directly extracted from the standard (translation and highlighted by me):
3.6.1.2 Function% co_of%
Implementations should not predefine main
. This function should not be overloaded. It must be declared with a return of type main
, but otherwise its type is defined by implementation. Implementations should allow:
A int
function returning ()
and
A int
function returning (int, puntero a puntero a char)
So your syntax error is because your int
function does not follow the standard. I bet even the error the compiler shows you is telling you that this function should return main
.
Regarding global variables.
Try not to use global variables :
-
Lack of location : The code is easier to understand when its scope is limited. Global variables can be read or modified from any part of the program, this makes it difficult to reason about their use or remember all the points in which they are used.
-
They lack access control or restriction verification : A global variable can be read or written from anywhere in the program, several rules about its use can be forgotten or violated.
-
Concurrency problems : If global variables can be accessed from multiple threads, it is necessary to synchronize them. When working with dynamically linked modules that use global variables, the resulting system may not be secure even when the modules are independent.
-
Namespace pollution : Global variables are in all contexts. You can end up using a global variable when you thought you were using a local or vice versa! (either by ignorance, misspelling the name or forgetting to create the local variable). Also, if you link modules with global variables whose name is the same, if you are lucky, you will have link errors ... if you are not lucky the linker will consider the variables as the same even if it was not your intention.
-
More link problems (in English) : I translated the points that I thought were most relevant to your problem.