I'm knocking my head against the wall. Here's what I want to do. I have a class called Record. I have another class called Field. A Record is made of multiple Fields, and each Field has several attributes.
In this code sample, all the field definitions are hard coded. At some point, they will be derived at run time, but that's not my issue right now. I'll typically only have 1 record.
Also, for indexing through this series of fields at runtime, I'm going to assume that I want to do pointer arithmetic to index through the array. I still need that definition too.
I can't figure out how to define my classes to get this thing to compile. Any advice is greatly appreciated.
(I could code this in Ruby with my eyes closed... )
Todd
In this code sample, all the field definitions are hard coded. At some point, they will be derived at run time, but that's not my issue right now. I'll typically only have 1 record.
Also, for indexing through this series of fields at runtime, I'm going to assume that I want to do pointer arithmetic to index through the array. I still need that definition too.
I can't figure out how to define my classes to get this thing to compile. Any advice is greatly appreciated.
(I could code this in Ruby with my eyes closed... )
Code:
#include <iostream>
using namespace std ;
// Field class definition
class Field {
int offset ; // Offset in the record to this field.
char datatype ; // Datatype of this field. char, num, dec, flt, dble
int len ; // Number of bytes occupied by this field in the input record
int maxlength ; // length of this field in the output record
int scale ; // If packed decimal, this is the number of digits to the right of the decimal point.
public:
void setv(int offset, int len, char dt, int maxlength, int scale) ; // set the values for the field.
int get_offset() ;
char get_datatype() ;
int get_len() ;
int get_maxlength() ;
int get_scale() ;
} ;
int Field::get_offset() { return offset ; }
char Field::get_datatype() { return datatype ; }
int Field::get_len() { return len ; }
int Field::get_maxlength() { return maxlength ; }
int Field::get_scale() { return scale ; }
// Record class definition
class Record {
public:
Field field[5] ; // I need an array of 5 elements.
Record() ; // Constructor
} ;
// Constructor for the Record class
Record::Record() {
cout << "Here in Record Constructor..." << endl ;
Field field[5] ; // An array of Fields.
field[0].setv( 0, 1, 'c' , 1, 0 ) ; // parms are: ( offset, length in bytes, data type,
field[1].setv( 1, 2, 'c' , 2, 0 ) ; // max length needed in output record, scale for decimal values)
field[2].setv( 3, 2, 'c' , 2, 0 ) ;
field[3].setv( 5, 2, 'c' , 2, 0 ) ;
field[4].setv( 7, 4, 'c' , 4, 0 ) ;
}
int main (int argc, char * const argv[]) {
Record rec ; // Initialize the Record & Field layouts
for (int i = 0 ; i < Record::field.length ; ++i) {
printf("Record %02d: offset=%02d, length=%02d, type=%c, Max Length=%02d, Scale=%02d\n",
i,
rec.field[i].get_offset() ,
rec.field[i].get_len() ,
rec.field[i].get_datatype() ,
rec.field[i].get_maxlength() ,
rec.field[i].get_scale() ) ;
}
return 0;
}
Todd