c++global-variablesblitz++

Blitz++ arrays as global arrays


I am working on a project in C++ which has many functions. I don't want to write them in the main program and would like to write a separate .cpp file for each of the functions. Most of these functions will act on some arrays, so I wish to make these arrays global. So I declared all the arrays in a separate .cpp file called globals.cpp and put them in a globals.h file prefixed with extern. The I wrote the functions and the main program as usual, however when I compile, I get an

Here is what I have:

//globals.cpp

#include <iostream>
#include <blitz/blitz.h>
#include <blitz/array.h>
#include "prototype.h"
#include "globals.h"

BZ_USING_NAMESPACE(blitz)

Array<double,2> A(5,5);

In the globals.h file I have

#ifndef GLOBALS_H
#define GLOBALS_H
extern Array<double,2> A(5,5);
#endif

Then I have a function add.cpp, for example

#include <iostream>
#include <blitz/blitz.h>
#include <blitz/array.h>
#include "prototype.h"
#include "globals.h"

BZ_USING_NAMESPACE(blitz)

void add.cpp(){
   for(int i=0;i<5;i++){
      A(i,i)=i*i;
   }
}

I obviously include it in the prototype.h file

#ifndef GLOBALS_H
#define GLOBALS_H
void add();
#endif

Finally I have the main program mainprog.c

#include <iostream>
#include <blitz/blitz.h>
#include <blitz/array.h>
#include "prototype.h"
#include "globals.h"

BZ_USING_NAMESPACE(blitz)
int main(){
   add();
   cout<<A<<endl;
   return 0;
}

However when I compile I get the error `globals.h:6:8: error: ‘Array’ does not name a type

and then an error in the add.cpp function saying the error A was not declared.

How do I declare the blitz arrays as global? Thank you `


Solution

  • The issue is that your macro to import the namespace(BZ_USING_NAMESPACE) is below your include of globals.h. Thus the Array class you are trying to reference in globals.h is actually blitz::Array or something at that point.

    For a simple fix, simply use the BZ_USING_NAMESPACE in globals.h right above your declaration for A.

    Always remember to include everything a header file needs in that header file.

    #ifndef GLOBALS_H
    #define GLOBALS_H
    
    #include <blitz/blitz.h> //No idea if the Array class needs this header.
    #include <blitz/array.h>
    BZ_USING_NAMESPACE(blitz)
    
    extern Array<double,2> A(5,5);
    #endif