Vector in nested struct

I have a nested record structure in C++ that contains a FileHeader record, a RecordHeader record and a DataRecord record. The last item is an array of unknown size (at compile time). I have to open a file and read the array size and then create the array.

I have worked on this for some time and can't figure out how to make it work. I know the sample data I am using has 85 records. I insert 85 into the array size and it works fine. But how do I create a dynamic array or define a vector within a nested structure?

1. What is the best (easiest) method to accomplish this (array or vector)?
2. How would it be implemented/how do you add data to it?

PreviousLogRecord.FaultRecord.push_back(field1); // does not work

PreviousLogRecord.FaultRecord[recordNumber].field1 = somedata; // works with 85 in array size.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
         struct LogFileHeaderRecordType
         {
             QString     field1;
             int         field2;
             QString     field3;
             int         field4;
         };

         struct LogHeaderRecordType
         {
             QString     field1;
             QString     field2;
             QString     field3;
             QString     field4;
         };

         struct LogFaultRecordType
         {
             int         field1;
             float       field2;
             float       field3;
             int         field4;
             int         field5;
             int         field6;
             int         field7;             
             int         field8;
         } ;

         struct LogRecordType
         {
             LogFileHeaderRecordType                     FileHeader;
             LogHeaderRecordType                         LogHeader;
             //LogFaultRecordType                        FaultRecord[85];
             std::vector <struct LogFaultRecordType>     FaultRecord;
         } PreviousLogRecord,  CurrentLogRecord;
Last edited on
closed account (N36fSL3A)
The same way you do outside a structure... you should drop the struct keyword out the vector...

1
2
3
4
5
6
7
8
LogFaultRecordType tmp;

tmp.field1 = 32235;
// ...

LogRecordType type;

type.FaultRecord.push_back(tmp);
Since it is a nested structure, shouldn't it be as follows?

 
PreviousLogRecord.FaultRecord.field1.push_back(tmp);
Last edited on
no, because field1 is an int, not a vector. FaultRecord is the vector.
Topic archived. No new replies allowed.