Hi all.
I would like to structurise my programm a little bit better. Therefore, I have in my mind to use one general main.cpp where I only call a lot of different subfunctions. All these subfunctions are initialized and collected in a header file subfuncttions.hpp. Additionally to that, there is a header file for all variables, GlobalVariables.hpp
I will fill these subfuntions with life in severel separated .cpp data files such that they can be ordered in a nice way.
ex to explain what I mean:
main.cpp:
1 2 3 4 5 6 7 8 9 10
#include <iostream>
#include "GlobalVariables.hpp"
#include "subfuncttions.hpp"
usingnamespace std;
int main()
{
variable_ = 5; // defined globally in "GlobalVariables.hpp"
subfunction1(); // call subfunction defined in "subfuncttions.hpp"
}
subfunctions.cpp:
1 2 3 4 5 6 7 8 9 10
#include <iostream>
// #include "GlobalVariables.hpp" // HERE IS THE POBLEM
#include "subfuncttions.hpp"
usingnamespace std;
void subfunction1()
{
cout << "TEST " << endl; ; // is working!
cout << variable_ << endl; // HERE IS THE PROBLEM
}
My problem is either there is a double definition of the variables when I insert #include "GlobalVariables.hpp", or the "globalVariable" variable_ is not known.
Is there any chance to keep my structure like that and solve the problem such that these variables are known in both .cpp data files?
#ifndef GlobalVariables
#define GlobalVariables
int variable_;
double variable2_
#endif
By the way, befor you wonder why I will use this structure for only two variable and und subfunction... For sure afterwards there will appear much more variables.
Question: Do you actually need these globals? Because if not (which is most likely the case), you should probably just remove those - or at least put them into a seperate namespace. It sounds like they are colliding with some other global defined in your version of <iostream>.
It would be much easier for me to use global variables!
But the problem is that I can in example rename them arbitrarely and they seem to collidate with iostream (even with strange variable names...) So that signalise me that the problem is lying somewhere else?
And, my compiler also try to show me were in iostream, variable_ is also defined, such that there are collidating but in the iostream sourcecode there is no such variable!!!
Yes, probably. Global Variables usually tend to make things harder rather than easier, but you are right in saying that if arbitrary names cause collisions then the problem probably lies somewhere else.
Btw, wrap your code between [ code ] [ /code ] tags for easier readability.
can you shortly explain what extern means (never see this before).
And in my case, what do you mean with GlobalVariables.cpp?
By only using extern int variable_ in the header, the variables in main.cpp and subfunctions.cpp are unknown.
"extern" tells the compiler to look up the definition of a variable somewhere else, and only makes sure that the name (and type) of the variable is known.
And in my case, what do you mean with GlobalVariables.cpp?
It means "put these lines in that file, and create the file if it doesn't exist". By adding this file to the build, the variables will become defined, but only once.