Can anyone help me with some C++ (Involves modules, and global variables) Need help bad!!

Jay59express

Senior member
Jun 7, 2000
481
0
0
I am writing my first homework that involves modules. We had written the program before just using different functions, but now since we are studying modules it has to include those. The problem that I am having is that the global varialbes that I declare (outside of main, in the client file):

const int MaxLength = 15;
const int MaxWords = 44;
const int MaxFileName = 30;

are not global to all of the seperate files correct? (2 other .h and .cpp files)
I tried creating the same variable declerations in the .h files with "extern" in front of them, but i am still getting error messages like this:

expected constant expression
cannot allocate an array of constant size 0
'<Unknown>' : missing subscript

These all have to do with arrays in the other 2 .cpp files try to access the global variables set in main.

So basically I need to know how/where to re-declare (if i need to) these global varialbes so that my other modules can have access to them.

Thanks for any help, and I hope my question is somewhat understandable.
 

gittyup

Diamond Member
Nov 7, 2000
5,036
0
0
In all the seperate files that you want to use your pre-defined constants, make them available in the other files by using the extern.

For example:

file1:
const int MaxLength = 15; //original declaration
const int MaxWords = 44; //original declaration
const int MaxFileName = 30; //original declaration

file2:
extern const int MaxLength; //defined earlier
extern const int MaxWords; //defined earlier
extern const int MaxFileName; //defined earlier

Keep in mind, in file2 above, the value of these extern variables is not known at compile time. Therefore, they can not be used to declare the size of arrays.