Connecting Tech Pros Worldwide Help | Site Map

array as member of class

=?ISO-8859-1?Q?Florian_B=FCrzle?=
Guest
 
Posts: n/a
#1: Aug 8 '07
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

Ian Collins
Guest
 
Posts: n/a
#2: Aug 8 '07

re: array as member of class


Florian Bürzle wrote:
Quote:
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.
Jim Langston
Guest
 
Posts: n/a
#3: Aug 8 '07

re: array as member of class


"Florian Bürzle" <fbuerzle@gmx.dewrote in message
news:46b989f7$0$5696$9b4e6d93@newsspool2.arcor-online.net...
Quote:
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).


Gianni Mariani
Guest
 
Posts: n/a
#4: Aug 8 '07

re: array as member of class


Florian Bürzle wrote:
Quote:
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
=?ISO-8859-1?Q?Florian_B=FCrzle?=
Guest
 
Posts: n/a
#5: Aug 8 '07

re: array as member of class


Ian Collins wrote:
Quote:
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
Jim Langston
Guest
 
Posts: n/a
#6: Aug 8 '07

re: array as member of class


"Florian Bürzle" <fbuerzle@gmx.dewrote in message
news:46b98f5d$0$20989$9b4e6d93@newsspool1.arcor-online.net...
Quote:
Ian Collins wrote:
Quote:
>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;
}


Gianni Mariani
Guest
 
Posts: n/a
#7: Aug 8 '07

re: array as member of class


Florian Bürzle wrote:
Quote:
Ian Collins wrote:
Quote:
>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";
}
------------
BobR
Guest
 
Posts: n/a
#8: Aug 8 '07

re: array as member of class



Jim Langston <tazmaster@rocketmail.comwrote in message...
Quote:
"Florian Bürzle" <fbuerzle@gmx.dewrote in message...
Quote:
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
*/
Quote:
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


=?ISO-8859-1?Q?Florian_B=FCrzle?=
Guest
 
Posts: n/a
#9: Aug 10 '07

re: array as member of class


Thanks to all for your help, especially for the code examples!

Florian
Closed Thread