declare array of objects in header file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philipp

    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(Lattice Parameters* 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::Lattic e(LatticeParame ters* 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


  • Allan Bruce

    #2
    Re: declare array of objects in header file


    "Philipp" <_NO_S~P~A~M_ki tschen@hotmail. com> wrote in message
    news:3f8ebb67$1 @epflnews.epfl. ch...[color=blue]
    >
    > Hello, I've got a class Lattice which is declared like that:
    >
    > // file: lattice.h
    >
    > class Lattice:public EventChooser{
    > public:
    > Lattice(Lattice Parameters* 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
    > };[/color]

    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::in it)

    HTH
    Allan


    Comment

    • Howard

      #3
      Re: declare array of objects in header file


      ">[color=blue]
      > LatticeSite = new lattice[x]; // declared elsewhere (i suggest
      > LatticeSite::in it)
      >[/color]

      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


      Comment

      • Philipp

        #4
        Re: declare array of objects in header file

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




        Comment

        • Allan Bruce

          #5
          Re: declare array of objects in header file


          "Howard" <alicebt@hotmai l.com> wrote in message
          news:bmmfpg$nrs @dispatch.conce ntric.net...[color=blue]
          >
          > ">[color=green]
          > > LatticeSite = new lattice[x]; // declared elsewhere (i suggest
          > > LatticeSite::in it)
          > >[/color]
          >
          > I take it you mean:
          >
          > lattice = new LatticeSite[x]; ?[/color]

          Yes I do - need some sleep!
          [color=blue]
          >
          >
          > 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?)
          >[/color]

          I meant x was declared somewhere so that the memory is allocated
          dynamically, but even better:

          // Lattice.h
          #include "LatticeSit e.h"

          class Lattice:public EventChooser
          {
          public:
          bool Init(int xiNumElements);
          ....
          private:
          LatticeSite* mLattice;
          };

          //Lattice.cpp
          bool Lattice::Init(i nt xiNumElements)
          {
          if ( (mLattice = new LatticeSite[xiNumElements]) == NULL)
          return true; // couldnt allocate the memory

          return false; // no problems :o)
          }

          I hope thats clear
          Allan


          Comment

          • Ron Natalie

            #6
            Re: declare array of objects in header file


            "Philipp" <_NO_S~P~A~M_ki tschen@hotmail. com> wrote in message news:3f8ec2ed$1 @epflnews.epfl. ch...[color=blue]
            > Hmm
            > I guess i can't allocate a static array with a variable as size.
            >
            > int N=2;
            > int a[N];
            >[/color]
            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.


            Comment

            Working...