• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

What's the proper way of declaring global variables in C++?

You declare globals outside of the main function. You're getting multiple definition errors because you're including that header file in more than one code file, which has the same effect as copying-and-pasting your code into multiple places and thus attempting to define the same global twice or more.

And wrong forum.
 
Originally posted by: TheLonelyPhoenix
You declare globals outside of the main function. You're getting multiple definition errors because you're including that header file in more than one code file, which has the same effect as copying-and-pasting your code into multiple places and thus attempting to define the same global twice or more.

And wrong forum.

If I declare the global outside of the main function, wouldn't i have the same problem? Because all the other files will need to include the file with the main function in order to use the variable.
 
Originally posted by: Vertimus
Originally posted by: TheLonelyPhoenix
You declare globals outside of the main function. You're getting multiple definition errors because you're including that header file in more than one code file, which has the same effect as copying-and-pasting your code into multiple places and thus attempting to define the same global twice or more.

And wrong forum.

If I declare the global outside of the main function, wouldn't i have the same problem?

No, because your error stems from the fact that you put it in a header file which you are using in more than one place. Just declare it right before the main function in your main.cpp for a quick workaround.
 
Originally posted by: TheLonelyPhoenix
Originally posted by: Vertimus
Originally posted by: TheLonelyPhoenix
You declare globals outside of the main function. You're getting multiple definition errors because you're including that header file in more than one code file, which has the same effect as copying-and-pasting your code into multiple places and thus attempting to define the same global twice or more.

And wrong forum.

If I declare the global outside of the main function, wouldn't i have the same problem?

No, because your error stems from the fact that you put it in a header file which you are using in more than one place. Just declare it right before the main function in your main.cpp for a quick workaround.

How would i use the variable in the other files then?
 
Originally posted by: Vertimus
Originally posted by: TheLonelyPhoenix
Originally posted by: Vertimus
Originally posted by: TheLonelyPhoenix
You declare globals outside of the main function. You're getting multiple definition errors because you're including that header file in more than one code file, which has the same effect as copying-and-pasting your code into multiple places and thus attempting to define the same global twice or more.

And wrong forum.

If I declare the global outside of the main function, wouldn't i have the same problem?

No, because your error stems from the fact that you put it in a header file which you are using in more than one place. Just declare it right before the main function in your main.cpp for a quick workaround.

How would i use the variable in the other files then?

This sounds like very, very poor code design. But then again, you're already using globals, so I guess you're not particularly worried about it.

Declare the global variable BEFORE the #include directive of the file(s) that use it. Then you're set.
 
Originally posted by: TheLonelyPhoenix
Originally posted by: Vertimus
Originally posted by: TheLonelyPhoenix
Originally posted by: Vertimus
Originally posted by: TheLonelyPhoenix
You declare globals outside of the main function. You're getting multiple definition errors because you're including that header file in more than one code file, which has the same effect as copying-and-pasting your code into multiple places and thus attempting to define the same global twice or more.

And wrong forum.

If I declare the global outside of the main function, wouldn't i have the same problem?

No, because your error stems from the fact that you put it in a header file which you are using in more than one place. Just declare it right before the main function in your main.cpp for a quick workaround.

How would i use the variable in the other files then?

This sounds like very, very poor code design. But then again, you're already using globals, so I guess you're not particularly worried about it.

Declare the global variable BEFORE the #include directive of the file(s) that use it. Then you're set.

aight, thanks 😛
 
extern type variable

goes into the header file that you are including

type variable

goes into the source code and not included within any function.

 
Back
Top