Home

"Florian Bürzle" <fbuerzle@gmx.de> wrote in message
news:46b989f7$0$5696$9b4e6d93@newsspool2.arcor-online.net...
> Hi! I've organized my code in different files - the declarations of
> global variables and methods are contained in a file global.hh (for
> compilation via makefile under Linux). In this file I included the
> following:
>
> extern const int dim; //dimension of the array
>
> class Particle{
> public:
> double x[dim]; //particle position
> };
>
>
> Since dim is defined in another file (where main() is located), the code
> does not compile. Does anyone have an idea how this could be fixed? I've
> consulted several books, but this doesn't seem to be a common problem
> (or I have the wrong books).

The compiler has to know what dim is when it gets to this line of code. It
needs to be a constant, but also knowable at compile time. Even if you
declared dim before you included the header, that would probably only work
in one of your compilation units. external linkage is that, linkage, not
compiling.

One solution: use a std::vector<double> isntead of an array. Have
Particle's constructor size it to fit (even using the global if you
absolutely must, but it would be better, IMO, to pass the value through the
constructor's paramater list).

previous
next

Re: An Editor that Skips to the End of a Def
Re: C/C++ guidelines
Re: Completely Lost...
Re: Extracting the traceback
Re: Use cout as ofstream object
Fundacja Avalon
Krwinka
Pajacyk
Fundacja Iskierka
Dzieci Niczyje