0

My main.cpp includes the header file setup.h.
setup.h reads data from a file, which is then used in main.cpp.
In the data file which setup.h reads, one piece of data is a single float variable, which I need to use in a function (from another header file) in main.cpp.

I can't use extern when defining the variable in my header file, because then I can't initialize it with the data from the file (gives an error when compiling):

setup.h

...
extern float G = 0; 
input >> G;
...

main.cpp

...
float G;
instance function(G);
...

How do I get the initialized variable in my main.cpp file?

Joshua
  • 999
  • 1
  • 6
  • 8
  • See previous stackoverflow question https://stackoverflow.com/questions/3627941/global-variable-within-multiple-files – pastaleg Nov 21 '17 at 22:45
  • For the sake of consistency I wanted all data to be read out by the same function in my header file, this solution would work but I'd need to add an extra .cpp file just to read out this value, which seems messy to me. – Joshua Nov 22 '17 at 09:28

2 Answers2

4

In the header you should just declare the variable so that the compiler knows that it exists somewhere:

extern float G;

In one of the compilation unit (cpp) you have to define the variable. You can then initalize it:

float G = 0.0;

Don't abuse of global variables ;-)

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • I wanted to avoid having to initialize it in the .cpp file because I want all my data to be read out with the function in the .h file, I guess that's not possible then? – Joshua Nov 22 '17 at 09:29
  • @Joshua no, indeed, that's not possible. The include is not an autonomous compilation unit. It is to be understood as if you would have its content in your cpp file, replacing the #include line. If you define your function in the header (i.e. with the function body), it would be defined in all the cpp files where you include it. For functions and (non extern) global variables, this could lead to linking conflicts. If you have a nice function that you'd like to reuse, declare it in the header (without the body), but define it in a separate cpp file. – Christophe Nov 22 '17 at 09:46
  • Thanks for the help, I'll be looking for another way to solve it then, since applying the answers I've gotten would require the user to specify the location of the data file twice. – Joshua Nov 22 '17 at 09:49
1

When you include a file using #include-directive, then you can think as if the content of the included file were inserted directly at the place of the #include-statement. That means that the content of the header file is not compiled on its own but in the course of the translation unit where it is included (and in the context of the place where it is included).

So your code would probably lead to...

extern float G = 0; 
input >> G;
float G;

which has several issues then.

First, variable G is defined twice; Note that extern together with an initializer does not just declare but actually define a variable (despite the extern-keyword). Second, statement input >> G is probably used at file scope, where it is not allowed (such code has to be placed inside a function).

So you the only thing you could do is...

setup.h

extern float G; // no initialisation here

setup.cpp

float init() {
  float dummy;
  cin >> dummy;
  return dummy;
}

float G = init(); // definition and initialization through a function

main.cpp

#include "setup.h"

int main() {
  cout << G;  // use of (initialized, externally defined) G
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
  • So there's no way to create and initialize a variable in the same header file and then use it in the main.cpp file? – Joshua Nov 22 '17 at 09:30