| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Philipp" |
| Date: |
16 Oct 2003 10:38:39 AM |
| Object: |
declare array of objects in header file |
Hello, I've got a class Lattice which is declared like that:
// file: lattice.h
class Lattice:public EventChooser{
public:
Lattice(LatticeParameters* params);
virtual ~Lattice();
// (...snip...)
private:
// How do I declare this member array in the lattice.h file?
// The following doesn't seem to work
LatticeSite** lattice; // does not compile
};
// file: lattice.cpp
Lattice::Lattice(LatticeParameters* params){
// Initializing 3D lattice
LatticeSite lattice[Ni][Nj][Nk];
for(int i=0; i<Ni; i++)
for(int j=0; j<Nj; j++)
for(int k=0; k<Nk; k++)
lattice[i][j][k].setValues(i,j,k,0);
}
The class LatticeSite has a default constructor.
So I don't know how to declare "lattice" which is an array of LatticeSite
objects in the file lattice.h.
Any ideas? Thanks Phil
.
|
|
| User: "Allan Bruce" |
|
| Title: Re: declare array of objects in header file |
16 Oct 2003 10:51:01 AM |
|
|
"Philipp" <_NO_S~P~A~M_kitschen@hotmail.com> wrote in message
news:3f8ebb67$1@epflnews.epfl.ch...
Hello, I've got a class Lattice which is declared like that:
// file: lattice.h
class Lattice:public EventChooser{
public:
Lattice(LatticeParameters* params);
virtual ~Lattice();
// (...snip...)
private:
// How do I declare this member array in the lattice.h file?
// The following doesn't seem to work
LatticeSite** lattice; // does not compile
};
Is LatticeSite declared in this header file? If not then remember to
#include it
BTW LatticeSite** is not setting an array, this is a pointer to a pointer to
a LatticeSite object. An array would be declared like
LatticeSite lattice[10];
However, if you dont know how many elements are required at compile time
then you will have to use a pointer and then initialise it using new, e.g.
LatticeSite *lattice; // declared in class definition
LatticeSite = new lattice[x]; // declared elsewhere (i suggest
LatticeSite::init)
HTH
Allan
.
|
|
|
| User: "Howard" |
|
| Title: Re: declare array of objects in header file |
16 Oct 2003 11:08:16 AM |
|
|
">
LatticeSite = new lattice[x]; // declared elsewhere (i suggest
LatticeSite::init)
I take it you mean:
lattice = new LatticeSite[x]; ?
And I have no idea what you mean by suggesting he "declare" it in a
(static?) init function...? (Are you referring to 'x' being assigned a
value elsewhere?)
-Howard
.
|
|
|
| User: "Allan Bruce" |
|
| Title: Re: declare array of objects in header file |
16 Oct 2003 11:56:57 AM |
|
|
"Howard" <alicebt@hotmail.com> wrote in message
news:bmmfpg$nrs@dispatch.concentric.net...
">
LatticeSite = new lattice[x]; // declared elsewhere (i suggest
LatticeSite::init)
I take it you mean:
lattice = new LatticeSite[x]; ?
Yes I do - need some sleep!
And I have no idea what you mean by suggesting he "declare" it in a
(static?) init function...? (Are you referring to 'x' being assigned a
value elsewhere?)
I meant x was declared somewhere so that the memory is allocated
dynamically, but even better:
// Lattice.h
#include "LatticeSite.h"
class Lattice:public EventChooser
{
public:
bool Init(int xiNumElements);
....
private:
LatticeSite* mLattice;
};
//Lattice.cpp
bool Lattice::Init(int xiNumElements)
{
if ( (mLattice = new LatticeSite[xiNumElements]) == NULL)
return true; // couldnt allocate the memory
return false; // no problems :o)
}
I hope thats clear
Allan
.
|
|
|
|
|
|
| User: "Philipp" |
|
| Title: Re: declare array of objects in header file |
16 Oct 2003 11:10:45 AM |
|
|
Hmm
I guess i can't allocate a static array with a variable as size.
int N=2;
int a[N];
won't work. Ok I'll to do do it dynamically then :-)
.
|
|
|
| User: "Ron Natalie" |
|
| Title: Re: declare array of objects in header file |
16 Oct 2003 01:08:48 PM |
|
|
"Philipp" <_NO_S~P~A~M_kitschen@hotmail.com> wrote in message news:3f8ec2ed$1@epflnews.epfl.ch...
Hmm
I guess i can't allocate a static array with a variable as size.
int N=2;
int a[N];
You can't declare an array anywhere with a given size that is not a constant expression.
vector<int> a(N);
would probably work fine for you.
.
|
|
|
|
|

|
Related Articles |
|
|