declare array of objects in header file



 DEVELOP > c-Plus-Plus > declare array of objects in header file

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
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.
.



  Page 1 of 1

1

 


Related Articles
how to declare an array of objects without "new"
Declare array of objects with constructor having arguments
How to modify a file using C++ file objects
Creating objects
WAS: why it seems that std::list::erase() doesnt free objects in a list, if the latter holds pointers to them?
Temporary objects as constructor arguments?
C++ program for generating combinatorial objects
A question about recursion and stacks of objects.
Global objects semantic with templates
Objects of the same class to access each other's private data
Virtual Destructors and array of objects
Re: GoF:"C++ [doesn't] treat classes as frist class objects"
Linker odd behaviour while trying to assign objects from 2 different namespace
LD errors when I link to archive, but not with the objects.
Method to Specify Uniqueness of Objects in an STL set?
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER