I declare and define several variables that should be read and write by all the other files in my program in a header called "variables.h".
variables.h
const int N;
const int M;
extern double x[M][N]; // x position of the particles
extern double y[M][N]; // y position of the particles
const double dt; // Time step size in MD simulation
const double totalStep; // Total number of MD steps
variables.cpp
N = 30;
M = 2;
dt = 1e-10; // Time step size in MD simulation
totalStep = 1e+7; // Total number of MD steps
double x[M][N];
double y[M][N];
Then I use these variables in "calc.cpp" with header "calc.h" like this:
calc.cpp
#include "calc.h"
#include "variables.h"
void myCalculation(double x[][N], double y[][N]){
for(int n = 0; n < M; n++){
for(int i = 0; i < N; i++){
double a; // This is just to show you guys
double b;
a = ( x[n][i+1] - x[n][i] );
b = ( y[n][i+1] - y[n][i] );
}
}
}
calc.h
void myCalculation(double x[][N], double y[][N]);
However, at this point I get the error: "use of undeclared identifier N in calc.h", since N is not defined there. When I include "variables.h" in "calc.h", I get "redefinition of variables" error. I assume it is related to using a header in a header file somehow.
How can use my array in my function in the "calc.h"?
#pragma once
to your headers.#define
instead ofconst int
x
,y
are hidden by parametersx
,y
inmyCalculation
. (andx
,y
are bad names for global variables).