initializing structure array

Discussion in 'C++' started by RB, May 3, 2010.

  1. RB

    RB Guest

    Hello I am trying to figure how to initialize structures within an array of structures.
    How do you do this ? Below is my pertinent code and futile attempts

    // -----in the class header file
    protected:
    // data holder structures
    struct Inlet
    { double dInvert_In_1;
    double dInvert_Out_1;
    unsigned int uiInletNum;
    };

    Inlet InletArray [200];

    public:
    // user input variables
    double m_Invert_In_1;
    double m_Invert_Out_1;
    UINT m_InletNum;
    //------------------------------
    //------in the class cpp file
    // in a class function
    Inlet MyInlet = { m_Invert_In_1, m_Invert_Out_1, m_InletNum}; //<-this compiles ok but does not get it into the array

    Inlet InletArray[m_InletNum] = { m_Invert_In_1, m_Invert_Out_1, m_InletNum}; //<- flags an error.
    RB, May 3, 2010
    #1
    1. Advertising

  2. On 5/3/2010 9:12 AM, RB wrote:
    > Hello I am trying to figure how to initialize structures within an array of structures.
    > How do you do this ? Below is my pertinent code and futile attempts
    >
    > // -----in the class header file


    It is always better to post real *compilable* code that we can copy and
    paste into our dev environment. Read the FAQ section 5 please before
    posting again.

    > protected:
    > // data holder structures
    > struct Inlet
    > { double dInvert_In_1;
    > double dInvert_Out_1;
    > unsigned int uiInletNum;
    > };
    >
    > Inlet InletArray [200];
    >
    > public:
    > // user input variables
    > double m_Invert_In_1;
    > double m_Invert_Out_1;
    > UINT m_InletNum;
    > //------------------------------
    > //------in the class cpp file
    > // in a class function
    > Inlet MyInlet = { m_Invert_In_1, m_Invert_Out_1, m_InletNum}; //<-this compiles ok but does not get it into the array
    >
    > Inlet InletArray[m_InletNum] = { m_Invert_In_1, m_Invert_Out_1, m_InletNum}; //<- flags an error.


    What error? If you read the FAQ section 5, we wouldn't have to waste
    time asking.

    Arrays cannot be initialized in the class constructors, unfortunately.
    There is more about them in the FAQ section 10 and section 34. What
    exactly are you trying to accomplish and where (which member function)?

    Read the FAQ please.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, May 3, 2010
    #2
    1. Advertising

  3. RB wrote:
    > Hello I am trying to figure how to initialize structures within an array of structures.
    > How do you do this ? Below is my pertinent code and futile attempts
    >
    > // -----in the class header file
    > protected:
    > // data holder structures
    > struct Inlet
    > { double dInvert_In_1;
    > double dInvert_Out_1;
    > unsigned int uiInletNum;
    > };
    >
    > Inlet InletArray [200];
    >
    > public:
    > // user input variables
    > double m_Invert_In_1;
    > double m_Invert_Out_1;
    > UINT m_InletNum;
    > //------------------------------
    > //------in the class cpp file
    > // in a class function
    > Inlet MyInlet = { m_Invert_In_1, m_Invert_Out_1, m_InletNum}; //<-this compiles ok but does not get it into the array
    >
    > Inlet InletArray[m_InletNum] = { m_Invert_In_1, m_Invert_Out_1, m_InletNum}; //<- flags an error.
    >


    Take a look at this:
    http://www.boost.org/doc/libs/1_42_0/libs/assign/doc/index.html
    Vladimir Jovic, May 3, 2010
    #3
  4. RB

    RB Guest

    Thnanks.
    RB, May 3, 2010
    #4
  5. RB

    RB Guest

    > What error? If you read the FAQ section 5, we wouldn't have to waste time asking.
    >

    Oh sorry, this is the first time I have visited this group in a long time and
    I didn't know where the FAQ was. I will go there now. Also here are
    the errors with 3 different attempt scenarios. It appears now that it is not so
    much the initialization that is the issue, but that I am trying to compile with
    variables that have not been "initialized?" yet . Hopefully you can see
    what I am trying to do with editbox values to be received from user input.
    I am somewhat at a loss as to how to implement this. Obviously I am
    not a professional programmer and again apologize for not showing errs.

    //These are edit box user input variables
    Inlet InletArray[m_InletNum] = { m_Invert_In_1, m_Invert_Out_1, m_InletNum};
    error C2057: expected constant expression
    error C2466: cannot allocate an array of constant size 0
    ----
    Inlet InletArray[0] = { 2, 2, 2};
    error C2466: cannot allocate an array of constant size 0
    -----
    Inlet InletArray[1] = { 2, 2, 2};
    0 error(s), 0 warning(s)
    RB, May 3, 2010
    #5
    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. Chandrashekar Tippur

    Initializing array of structure!!

    Chandrashekar Tippur, Nov 19, 2004, in forum: C Programming
    Replies:
    7
    Views:
    1,892
    Jack Klein
    Nov 20, 2004
  2. rashmi
    Replies:
    2
    Views:
    461
    Lawrence Kirby
    Sep 20, 2005
  3. ScottM
    Replies:
    5
    Views:
    409
    ScottM
    Jul 11, 2006
  4. vduber6er
    Replies:
    3
    Views:
    513
    Frederick Gotham
    Oct 24, 2006
  5. Mik0b0
    Replies:
    12
    Views:
    501
    David Thompson
    Apr 15, 2007
Loading...

Share This Page