Splitting large cpp into seperate files (Problem with declaring a global variable)

Joined
Aug 25, 2008
Messages
1
Reaction score
0
Hi all,
I have a single 'main.cpp' file and another 'main.h'. The '.cpp' contains lots of modules used throughout the program and everything works fine. I want to split this huge code into several .cpp's. My problem is with declaring a global variable. To illustrate,

Code:
"main.h"
extern const int N;
extern const int R;
double u[3][N][R];
------------------
"main.cpp"
include <main.h>
const int N = 10; 
const int R = 2;

int main(){

for (int j=0;j < R;j++){
     for (int i=0;i < N;i++){
         fun1(i,j);
}
}
more calculation done here...

return 0;
}
------------------
fun1.cpp     <---- Wanting to create
include <main.h>

double fun1(int i,int j){
u[0][i][j] = calculations....

// same goes for u[1][i][j] and u[2][i][j]

return u[0][i][j];
return u[1][i][j];
return u[2][i][j];
}
some calculation done ....

Obviously, declaring u in the header file like that will not work since N and R are still not initialized. I want to find the proper way to declare u and being identifed in fun1. I understand that I could define u as a double variable instead of this 3D array but I want to store all the u values for further calculations.

Sorry if this is a silly question but I have searching for hours with no luck so I appreicate any help. Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top