declare array of objects in header file

Discussion in 'C++' started by Philipp, Oct 16, 2003.

  1. Philipp

    Philipp Guest

    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
     
    Philipp, Oct 16, 2003
    #1
    1. Advertising

  2. Philipp

    Allan Bruce Guest

    "Philipp" <_NO_S~P~A~> wrote in message
    news:3f8ebb67$...
    >
    > 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
     
    Allan Bruce, Oct 16, 2003
    #2
    1. Advertising

  3. Philipp

    Howard Guest

    ">
    > 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
     
    Howard, Oct 16, 2003
    #3
  4. Philipp

    Philipp Guest

    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 :)
     
    Philipp, Oct 16, 2003
    #4
  5. Philipp

    Allan Bruce Guest

    "Howard" <> wrote in message
    news:bmmfpg$...
    >
    > ">
    > > 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
     
    Allan Bruce, Oct 16, 2003
    #5
  6. Philipp

    Ron Natalie Guest

    "Philipp" <_NO_S~P~A~> wrote in message news:3f8ec2ed$...
    > 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.
     
    Ron Natalie, Oct 16, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    16
    Views:
    511
    Mike Smith
    Jul 11, 2005
  2. Replies:
    7
    Views:
    1,442
    Jakob Bieling
    Jul 21, 2005
  3. thinktwice
    Replies:
    4
    Views:
    463
    Alf P. Steinbach
    Jan 13, 2006
  4. beet
    Replies:
    5
    Views:
    552
  5. beet
    Replies:
    2
    Views:
    1,978
    Jim Langston
    Jun 17, 2008
Loading...

Share This Page