0

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"?

9
  • Add include guards or #pragma once to your headers. Commented Oct 10, 2014 at 7:12
  • Use #define instead of const int Commented Oct 10, 2014 at 7:15
  • rename to variables.cpp, and compile that file too (for the doubles/ints)
    – Exceptyon
    Commented Oct 10, 2014 at 7:36
  • [OT]: your global x, y are hidden by parameters x, y in myCalculation. (and x, y are bad names for global variables).
    – Jarod42
    Commented Oct 10, 2014 at 7:55
  • @JonathanPotter I should do in variables.h : #define N 30 and then what should I do in calc.h? Do I need to include variables.h again there?
    – melampyge
    Commented Oct 10, 2014 at 8:11

1 Answer 1

0

order of declaration is not good, define N, M before x, y:

const int N = 30;
const int M = 2;
extern double x[M][N]; // x position of the particles
extern double y[M][N]; // y position of the particles

And any file using N, M should include variables.h as calc.h.

And your header should have include guards

so

#ifndef VARIABLE_H
#define VARIABLE_H

// your code

#endif

or you may use #pragma once (but it is not standard :-( ).

1
  • Oh, this is why I get downvoted, I always screw up when I am trying to write a working example of the crash :). I am so sorry if I misled you but that's a mistake that I did when I translate my code here, in my original code it was correct. The problem appears to be that when I include variables.h in calc.h, the compiler says that there are 2 definitions for each variable.
    – melampyge
    Commented Oct 10, 2014 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.