In several different header file, there are different value defined to a same name.
For example, in header file "listing.h", there's code
#define MAXNUMBER 120
And in header file "energyCalc.h", there's code
#define MAXNUMBER 50
If in source file, there's also a definition of MAXNUMBER, and it's somewhere before the inclusion of those two headers. The MAXNUMBER will be wrong in the source file.
It happens because the scope of define is the whole source file. They may conflict when the program is running. Is there any way to limit the scope of a define, so that they may not conflict against each other?
Prefer using the constkeyword instead of #define : constint MAXNUMBER = 50; That way you will get a compile error if you try to define multiple constants with the same name.
To avoid collision you can use different names for the two constants. You can also put them into different scopes e.g. inside a namespace or class, and still have the same name for the two constants.
for example you want to restrict MAXNUMBER in the file it is used:
1 2 3 4 5 6
#define MAXNUMBER 50
/* some code...
...
*/
#undef MAXNUMBER
// here MAXNUMBER will be undefined
But i just noticed it doesn't solve your problem, do as Peter87 said or define MAXNUMBER after all header inclusions to make sure it has the right value
By using #undef you can still make sure the macro you defined will not be used uninitialized in another place.