copying data between (similar) classes

I have two classes which are essentially the same, only with different array sizes.
ie:
1
2
3
4
5
6
7
8
9
class A{
       char character;
       short shortArray[1201];
} a[1201];

class B{
       char character;
       short shortArray[3601];
} b[3601];


I would like to either have an overloaded operator= or a function so that "b = a" or b.function(a) will make
1
2
3
4
b[0].character=a[0].character
b[0].shortArray[0]=a[0].shortArray[0]
b[0].shortArray[3]=a[0].shortArray[1]
...

I've tried:
1
2
3
4
5
6
B B::operator=(A& aAddress){
       character = aAddress.character;
       for ( int i=0; i<1201; i++){
              shortArray[i] = aAddress.character[i*3];
       } // for ( int i=0; i<1201; i++)
}

with
b = a;
which gives an error of
incompatible types in assignment of 'A[1201]' to 'B[3601]'
or
incompatible types in assignment of 'A[1201]' to 'B[1201]'
if i make the class array the same size.

When I tried writing a function as a member of B, it could not access the members of A, regardless of if they were private/protected (after reading http://www.cplusplus.com/doc/tutorial/classes/ and http://www.cplusplus.com/doc/tutorial/classes2/ )

Is anyone able to offer some assistance?

Thanks in advance :)
Your class declarations seem odd. Aren't you actually declaring a as an array of 1201 objects of type A, and declaring b as an array of 3601 objects of type B?

If so, a is not of type class A, but of type class A *. Same goes for b. I think you did not intend to have those as arrays.

Try:
1
2
3
4
5
6
7
8
9
class A{
       char character;
       short shortArray[1201];
} a;

class B{
       char character;
       short shortArray[3601];
} b;


Also note that 1201 * 3 =3603, not 3601. It will fit with 3601, but note that the last element will be lacking two empty trailing slots.

Also note that the shortArray array of class B is larger than the array from class A. I think you have them backwards in your operator overload.
Thank you for the response.

The class is for "data records" in two different levels of Digital Terrain Elevation Data, so without getting into the nitty-gritty of DTED, I need an array of records, so that was intentional. I did pick up on the 3601/3603 issue and I'm racking my brain about how to better deal with it, but I wanted to make sure the classes worked first.

My mistake in the previous post, it should have been
1
2
3
4
5
6
B B::operator=(A& aAddress){
       character = aAddress.character;
       for ( int i=0; i<1201; i++){
              shortArray[i*3] = aAddress.character[i];
       } // for ( int i=0; i<1201; i++)
}


should I pass it a pointer rather than an address? I've always been a bit sketchy about the two.
ie:(?)
1
2
3
4
5
6
B B::operator=(A* aPointer){
       character = aPointer.character;
       for ( int i=0; i<1201; i++){
              shortArray[i*3] = aPointer.character[i];
       } // for ( int i=0; i<1201; i++)
}


How would I incorporate the class arrays into this?
So you need to copy 1201 values from one class to another, 1201 times over? Did I get it right now?

If so, your first operator is probably OK, but you'll need to iterate through the arrays one element at the time.

1
2
3
4
for (int i = 0; i <1201; i++)
{
    b[3 * i] = a[i];
}


I do think, however, that such a simple for-loop shouldn't give you too much of a hard time.
I'm not sure that I would agree with the idea of using the assignment operator for this purpose. that is a special kind of function that is supposed to be used for assignments of the same type. Did you ever over come the protection issues? How does type b get direct access to type a privates or vice versa?

Also, what is the meaning of b = a? The array sizes aren't the same so in my humble opinion, this operation makes no logical sense. After the completion of the operation, there is no possible way that b == a.
That's what I need to do, essentially a 1201x1201 matrix of elevation values, but with metadata before and after each block of 1201 elevation values >_<
The basic file data structure is 3428 bytes of metadata regarding the file followed by 1201 lots of:
8 bytes metadata, 2402 bytes of elevation values (1201*16bit values), 4 bytes checksum.

All of this is stored in a *.DT1 file. I'm reading this file as a char array using fstream then bit shifting the two char pairs into a single short to get the elevation as a number. I've made a class which contains the char array, short array and functions to convert from one to the other (and also print the elevations just to double check I'm getting the right data...)

The aim of my program is to convert from *.DT1 to *.DT2 which has a data structure of 3428 bytes of file header/metadata followed by 3601 lots of:
8 bytes metadata, 7202 bytes of elevation values (3601*16bit values), 4 bytes checksum.

So I've made another class for the level 2 data and just need a way of extrapolating level 1 to level 2, then I'll average the elevation values to fill in the gaps. Should be fairly simple... Right?

I started out writing the operator= overload as a function of class B which didn't work, but maybe will now I've added each class as the others friend?

Thanks for all your help so far (: It's compilable, just crashes when trying to convert... *sigh*
Topic archived. No new replies allowed.