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,
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
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