Wierd Error
-
I am getting these Errors:
Whenever I Define a Variable in the "libraries.h" header filebool TextEditChangesSaved;
This line is causing these errors, but when I make it a comment (or delete it)
like here:
it works fine, and the program runs successfully, why am I getting this error and how can I fix it.
Note: that these libraries.h file is included in all the files, and whenever I make a variable there, I get these errors.
Thank you very much! -
I don't see what's weird here - it's a basic c thing. You define a variable in an header so it's created in every compilation unit the header is included. You must only declare it in the header and define it once in the source (one definition rule) --> see e.g. https://www.geeksforgeeks.org/understanding-extern-keyword-in-c/
-
I don't see what's weird here - it's a basic c thing. You define a variable in an header so it's created in every compilation unit the header is included. You must only declare it in the header and define it once in the source (one definition rule) --> see e.g. https://www.geeksforgeeks.org/understanding-extern-keyword-in-c/
@Christian-Ehrlicher can you please tell me, in my case, how will the declaration be? Like what should I write in this header file and what should I write in other places, I need to use this variable in
-
@Christian-Ehrlicher can you please tell me, in my case, how will the declaration be? Like what should I write in this header file and what should I write in other places, I need to use this variable in
@Yousef-Alaa-Hussain
What do you want to know that is not covered in the link @Christian-Ehrlicher gave you? How else do you want it expressed? In a C/C++ program you can have one occurrence (in some.c
/.cpp
file, top-level) of:int global_variable;
In every other file which wants to access it you need
extern int global_variable;
Which you might put in a common
.h
file if you want it accessible in several other files. -
@Yousef-Alaa-Hussain
What do you want to know that is not covered in the link @Christian-Ehrlicher gave you? How else do you want it expressed? In a C/C++ program you can have one occurrence (in some.c
/.cpp
file, top-level) of:int global_variable;
In every other file which wants to access it you need
extern int global_variable;
Which you might put in a common
.h
file if you want it accessible in several other files.@JonB ok, thanks.
I didn't understand where should I write the global variable, and didn't understand where should I write the extern word,
but now I can understand so much! Thank you! And for @Christian-Ehrlicher -