initializing structure array

R

RB

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

Victor Bazarov

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
 
V

Vladimir Jovic

RB said:
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
 
R

RB

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top