Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > declare array of objects in header file

Reply
Thread Tools

declare array of objects in header file

 
 
Philipp
Guest
Posts: n/a
 
      10-16-2003

Hello, I've got a class Lattice which is declared like that:

// file: lattice.h

class Latticeublic 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


 
Reply With Quote
 
 
 
 
Allan Bruce
Guest
Posts: n/a
 
      10-16-2003

"Philipp" <_NO_S~P~A~(E-Mail Removed)> wrote in message
news:3f8ebb67$(E-Mail Removed)...
>
> Hello, I've got a class Lattice which is declared like that:
>
> // file: lattice.h
>
> class Latticeublic 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


 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      10-16-2003

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


 
Reply With Quote
 
Philipp
Guest
Posts: n/a
 
      10-16-2003
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




 
Reply With Quote
 
Allan Bruce
Guest
Posts: n/a
 
      10-16-2003

"Howard" <(E-Mail Removed)> wrote in message
news:bmmfpg$(E-Mail Removed)...
>
> ">
> > 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 Latticeublic 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 )
}

I hope thats clear
Allan


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      10-16-2003

"Philipp" <_NO_S~P~A~(E-Mail Removed)> wrote in message news:3f8ec2ed$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Header files with "header.h" or <header.h> ?? mlt C++ 2 01-31-2009 02:54 PM
declare a struct with c++ vectors in a header file... beet C++ 5 06-17-2008 08:23 PM
how could i declare & define function in header file thinktwice C++ 4 01-13-2006 07:18 AM
Declare array of objects with constructor having arguments b83503104@yahoo.com C++ 7 07-21-2005 06:41 AM
how to declare an array of objects without "new" b83503104@yahoo.com C++ 16 07-11-2005 04:14 PM



Advertisments