Reputation: 23
I am quite new to C++, so if there is a easier way to get what I want, feel free to tell me...
I've got a header containing constants I need to include everywhere in my code to use them in equations. Stuff like temperature and pressure.. Before I was using a single object with a constant mass.
real massObject = 7.35619e-25;
Now I want to have more than one mass to be able to use more objects. So I tried to define Elements of an array I created.
const int numObjects = 1;
double vmassObject[numObjects];
vmassObject[0] = 7.35619e-25;
Then I found out that it was not possible to define every Element outside a function but I don't want to use a function because I would have to call it everytime. Also passing is no option. Is there a way to define the elements globally?
Thanks
Upvotes: 2
Views: 1366
Reputation: 409166
You can initialize the array:
double vmassObject[numObjects] = {
7.35619e-25
};
On a related note, you can't put this in a header file that you include in multiple source files. That's because then the array would be defined multiple times, and you can only have one definition in a program.
In the header you can declare the array:
extern double vmassObject[numObjects];
Then put the definition (with the initialization) in a single source file.
Upvotes: 2