By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,601 Members | 1,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,601 IT Pros & Developers. It's quick & easy.

array as member of class

P: n/a
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).

Thanks in advance for any advice!

Florian

Aug 8 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Florian Bürzle wrote:
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 array size has to be a compile time constant, it has to be known
when each translation unit that include the header is compiled.

You can either put the value in the header (say a static const unsigned
member), or use a dynamic array.

--
Ian Collins.
Aug 8 '07 #2

P: n/a
"Florian Bürzle" <fb******@gmx.dewrote in message
news:46**********************@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<doubleisntead 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).
Aug 8 '07 #3

P: n/a
Florian Bürzle wrote:
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).

Thanks in advance for any advice!
Two options.
a) Place the "const int dim = N" in the header.
b) make Particle::x a std::vector
Aug 8 '07 #4

P: n/a
Ian Collins wrote:
The array size has to be a compile time constant, it has to be known
when each translation unit that include the header is compiled.

You can either put the value in the header (say a static const unsigned
member), or use a dynamic array.
Thanks for the quick reply. Can you give me a hint how to implement such
a dynamic array in this case? (Sorry if this is trivial, but I've never
used dynamic arrays).

Thanks!

Florian
Aug 8 '07 #5

P: n/a
"Florian Bürzle" <fb******@gmx.dewrote in message
news:46***********************@newsspool1.arcor-online.net...
Ian Collins wrote:
>The array size has to be a compile time constant, it has to be known
when each translation unit that include the header is compiled.

You can either put the value in the header (say a static const unsigned
member), or use a dynamic array.

Thanks for the quick reply. Can you give me a hint how to implement such
a dynamic array in this case? (Sorry if this is trivial, but I've never
used dynamic arrays).
Pick one. My preference is Foo.

#include <iostream>
#include <vector>

extern const int dim; //dimension of the array

const int dim = 10;

class Foo
{
public:
Foo( const int Size )
{
x.resize( Size );
}
std::vector<doublex;
};

class Foo2
{
public:
Foo2()
{
x.resize( dim );
}
std::vector<doublex;
};

class Bar
{
public:
Bar( const int Size ) { x = new double[ Size ]; }
~Bar() { delete[] x; }
double* x;
};

class Bar2
{
public:
Bar2() { x = new double[ dim ]; }
~Bar2() { delete[] x; }
double* x;
};

int main()
{
Foo foo( dim );
Foo2 foo2;
Bar bar( dim );
Bar2 bar2;

return 0;
}
Aug 8 '07 #6

P: n/a
Florian Bürzle wrote:
Ian Collins wrote:
>The array size has to be a compile time constant, it has to be known
when each translation unit that include the header is compiled.

You can either put the value in the header (say a static const unsigned
member), or use a dynamic array.

Thanks for the quick reply. Can you give me a hint how to implement such
a dynamic array in this case? (Sorry if this is trivial, but I've never
used dynamic arrays).
Here is some example code.

------------
#include <vector>

// VectDim is basically a vector with a default dimension
// given by a const int reference
template <typename T, const int & D>
class VectDim : public std::vector<T>
{
public:
VectDim() : std::vector<T>( D )
{
}
};

extern const int dim;

class Particle{
public:
VectDim<double,dimx; //particle position
};
const int dim = 4;

#include <iostream>

int main()
{
Particle p;

std::cout << p.x.size() << "\n";
}
------------
Aug 8 '07 #7

P: n/a

Jim Langston <ta*******@rocketmail.comwrote in message...
"Florian Bürzle" <fb******@gmx.dewrote in message...
Thanks for the quick reply. Can you give me a hint how to implement such
a dynamic array in this case? (Sorry if this is trivial, but I've never
used dynamic arrays).

Pick one. My preference is Foo.

#include <iostream>
#include <vector>

extern const int dim; // dimension of the array
const int dim = 10;

class Foo
{
public:
Foo( const int Size )
{
x.resize( Size );
}
std::vector<doublex;
};
I prefer 'initialization lists':

class Foo{ public:
Foo( size_t const Size ) : x( Size, 3.14 ){} // OP, note the colon
Foo() : x( dim ){} // 'dim' doubles inited to zero (Foo2 below)
std::vector<doublex;
};
/* - main() or ? -
Foo aFoo( 7 );
std::cout<<" aFoo.x.at(2)="<<aFoo.x.at(2)<<std::endl;
// out: aFoo.x.at(2)=3.140000
*/
class Foo2{ public: // shortened to *my* style for NG post
Foo2(){ x.resize( dim ); }
std::vector<doublex;
};

class Bar{ public:
Bar( const int Size ) { x = new double[ Size ]; }
~Bar() { delete[] x; }
double* x;
};
class Bar{ public:
Bar( size_t const Size ) : x( new double[ Size ] ){}
~Bar() { delete[] x; }
double *x;
};
// 'function level try block' for Ctor not shown.

// Why 'size_t' (unsigned integer type)?
class Bar{ public:
// Bar( int const Size ) : x( new double[ Size ] ){}
// use: Bar aBar( -1 ); runtime-error: bad_alloc

Bar( size_t const Size ) : x( new double[ Size ] ){}
// use: Bar aBar( -1 ); compile-warning
// [Warning] passing negative value `-1'
// for argument 1 of `Bar::Bar(unsigned int)'
// == a HUGE size

~Bar(){ delete[] x; }
double *x;
};

[ snip other examples ]

OP: Jim was keeping it simple so not to confuse you. Look over his post
before you tackle the 'init list' modifications I've shown.
The 'initialization lists; are the prefered way to do it, when you can
(sometimes you must use the body of the constructor (like to set init values
in array[])).
More questions? No problem, ask away.

--
Bob R
POVrookie
Aug 8 '07 #8

P: n/a
Thanks to all for your help, especially for the code examples!

Florian
Aug 10 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.