declare array of objects in header file

P

Philipp

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[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
 
A

Allan Bruce

Philipp said:
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
 
H

Howard

">
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
 
P

Philipp

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 :)
 
A

Allan Bruce

Howard said:
">
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 :eek:)
}

I hope thats clear
Allan
 
R

Ron Natalie

Philipp said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top