Hello
I've just started using c++ so I'm learning as I go and doing lots of web searches. I'm trying to split my code into separate files as it's gotten quite large and unwieldy. I am using global variables in my code, which I know is frowned upon, but I have some very large arrays which I don't see how I can pass between functions without running out of memory.
I've managed to get the split-up code working when compiled, but I get highlighted errors in the code because of variables not being defined in the included functions. Let me show you an example. My main code looks like this.
1 2 3 4 5 6 7 8
|
#include <iostream>
using namespace std;
int VeryLargeGlobalArray[100]; //define very large global array
#include "OutputArray.h" //include output array function
int main() {
OutputArray(50);
}
|
And my included file, OutputArray.h, is this:
1 2 3 4 5 6
|
#include <iostream>
void OutputArray(int LastIndex){
for(int i=0; i<=LastIndex; i++){
cout << i << ": " << VeryLargeGlobalArray[i] << "\n";
}
}
|
In this separate header file "cout" is underlined as it doesn't recognise the command and "VeryLargeGlobalArray" is also underlined as undefined.
I'm sure I'm approaching this completely wrong. I probably shouldn't be using header files for this, but I couldn't get it to work with cpp files. I've tried web searches but I can't seem to find a clear answer. Can someone set me straight?
I'm on Windows 10 using Visual Studio Code, my compiler is MinGW-W64.
Apologies if this has been covered elsewhere, but I've been unable to find an explanation that makes sense to me with my current level of knowledge.
Many thanks.