471,584 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,584 software developers and data experts.

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
Jul 19 '05 #1
5 12470

"Philipp" <_N******************@hotmail.com> wrote in message
news:3f********@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
Jul 19 '05 #2

">
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
Jul 19 '05 #3
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 :-)


Jul 19 '05 #4

"Howard" <al*****@hotmail.com> wrote in message
news:bm********@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
Jul 19 '05 #5

"Philipp" <_N******************@hotmail.com> wrote in message news:3f********@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.
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Secondpost | last post: by
1 post views Thread by hepp | last post: by
15 posts views Thread by Geoff Cox | last post: by
8 posts views Thread by redefined.horizons | last post: by
4 posts views Thread by Rui.Hu719 | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.