Class Template problem

I'm trying to learn c++ using a book, and I must write a simple template class that holds two arrays of types that are defined via the class’s template parameter list. The size of the array is 10, and the template class
should have accessor functions that allow for the manipulation of array elements.

The source code I made looks like this:

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
36
37
38
39
40
#include <iostream>
using namespace std;

template <typename Array1Type, typename Array2Type>
class Arrays
{
private:
    Array1Type MyArray1 [10];
    Array2Type MyArray2 [10];
public:
    //constructor that initializes member variables
    Arrays (const Array1Type& Array1, const Array2Type& Array2)
    {
        MyArray1 [10] = Array1;
        MyArray2 [10] = Array2;
    }

    //accesor functions
    Array1Type& GetElemArray1(int Index) {return MyArray1[Index];}
    Array2Type& GetElemArray2 (int Index) {return MyArray2[Index];}
};
int main()
{
    //declaring and initializing first array so it doesn't hold random crap
    int Array1 [10] = {0,1,2,3,4,5,6,7,8,9};
    //declaring and initializing second array so it doesn't hold random crap
    char Array2 [10] = {'a', 'b','c','d', 'e','f','g','h','i','j'};
    //constructing object TwoArrays of type Arrays
    Arrays <int,char> TwoArrays (Array1[10], Array2[10]);
    //reading integer used to determine element position
    cout <<"Insert the zero base index of the element you want to see from first array: ";
    int Index = 0;
    cin >> Index;
    //writing the Index-element
    cout << "The element Array1[" <<Index<<"]= "<<TwoArrays.GetElemArray1(Index)<<endl;
    cout <<"Insert the zero base index of the element you want to see from second array: ";
    cin >> Index;
    cout << "The element Array2[" <<Index<<"]= "<<TwoArrays.GetElemArray2(Index)<<endl;
    return 0;
}


The problem is that when I use accessor functions in main(), the output written on console after I provide the Index, looks like some random crap, not arrays' element defined by me. It looks like this:

1
2
3
4
5
6
7
Insert the zero base index of the element you want to see from first array: 1
The element Array1[1]= 1970992338
Insert the zero base index of the element you want to see from second array: 2
The element Array2[2]= (

Process returned 0 (0x0)   execution time : 4.598 s
Press any key to continue.



What is wrong?
You are passing the elements at index 10 (out of bounds!) in the arrays to the TwoArrays<int,char> constructor. In the constructor you set the elements at index 10 (out of bounds again!).
Arrays <int,char> TwoArrays (Array1[10], Array2[10]);

This is the problem. This doesn't pass an array, it passes the element at index 10.

Since the indices go from 0 to n-1, 10 is out of bounds. Additionally, it is a single value, not an array. Try using this as your constructor implementation. Note: the additional size parameters. This is an example of what you would like to do.

From the declaration:

1
2
3
4
5
6
7
8
9
10
11
template<typename T, typename U>
class doubleArray
{
// function decs

private:
    T *c_arrayOne;
    U *c_arrayTwo;
    const unsigned int c_sizeOne;
    const unsigned int c_sizeTwo;
};


From the implementation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    
        template<typename T, typename U>
        doubleArray(const T *arrayOne, const unsigned int &arrayOneSize,
                const U *arrayTwo, const unsigned int &arrayTwoSize):
        c_sizeOne(arrayOneSize),
        c_sizeTwo(arrayTwoSize)
    {
        for(unsigned int i = 0; i < c_sizeOne; i++)
        {
            c_arrayOne[i] = arrayOne[i];
        }
        for(unsigned int i = 0; i < c_sizeTwo; i++)
        {
            c_arrayTwo[i] = arrayTwo[i];
        }
    }



You can at later point use initializer lists to initialize the array, however for now, this approach will do what you are after. Initializer lists for array elements reduce overhead significantly.

An example of a valid call to this constructor:
1
2
3
4
    int arrayOne [10] = {0,1,2,3,4,5,6,7,8,9};
    char arrayTwo [10] = {'a', 'b','c','d', 'e','f','g','h','i','j'};

    doubleArray<int, char> myArray(arrayOne, 10, arrayTwo, 10);


You should, as well, store the size of the arrays within the class.

ps: It is 2am here, so this may not be exactly right, but it should illustrate the issue.

Edit: Used wrong type. I think its fixed now.
Last edited on
Thank you for the quick reply. The final version of my program looks like this:

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
using namespace std;

template <typename Array1Type, typename Array2Type>
class Arrays
{
private:
    //declaring array sizes and arrays as private attributes
    const unsigned int c_size1;
    const unsigned int c_size2;
    Array1Type* MyArray1;
    Array2Type* MyArray2;
public:
    //constructor that initializes member variables
    Arrays (const Array1Type* Array1, const unsigned int &size1, const Array2Type* Array2, const unsigned int &size2)
            :c_size1(size1), c_size2(size2)
    {
        for (unsigned int i = 0; i < c_size1; ++i)
        {
            MyArray1 [i] = Array1 [i];
        }
        for (unsigned int i = 0; i < c_size2; ++i)
        {
            MyArray2 [i] = Array2 [i];
        }
    }

    //accesor functions
    Array1Type& GetElemArray1(int Index) {return MyArray1[Index];}
    Array2Type& GetElemArray2 (int Index) {return MyArray2[Index];}
};
int main()
{
    //declaring and defining first array
    int IntArray[10] = {0,1,2,3,4,5,6,7,8,9};
    //declaring and defining second array
    char CharArray [10] = {'a', 'b','c','d', 'e','f','g','h','i','j'};
    //constructing object TwoArrays of type Arrays
    Arrays <int,char> TwoArrays (IntArray, 10, CharArray, 10);
    //reading integer used to determine element position
    cout <<"Insert the zero base index of the element you want to see from first array: ";
    int Index = 0;
    cin >> Index;
    //writing the Index-element
    cout << "The element IntArray[" <<Index<<"]= "<<TwoArrays.GetElemArray1(Index)<<endl;
    cout <<"Insert the zero base index of the element you want to see from second array: ";
    cin >> Index;
    cout << "The element CharArray[" <<Index<<"]= "<<TwoArrays.GetElemArray2(Index)<<endl;
    return 0;
}


Even if the compiler sees no errors, I think I have some runtime errors: The output looks like this:
1
2
3
4
5
6
7
Insert the zero base index of the element you want to see from first array: 6
The element IntArray[6]= 2686728
Insert the zero base index of the element you want to see from second array: 3
The element CharArray[3]= d

Process returned -1073741819 (0xC0000005)   execution time : 10.379 s
Press any key to continue.


So, the 7th element (index = 6) from IntArray contains random crap. The output for second array looks ok. And the process returnes an error.

I have a few questions:
1. Why does accessor function (GetElemArray1) of MyArray1 returnes random numbers, even if I initialized IntArray ?The element IntArray[6]= 2686728 i guess it has something to do with pointers...
2. Why do I have to declare an array member like this: Array1Type* MyArray1;, using pointers. Is there any other way to define it? something like int MyArray1[10]?
3. What else is wrong or is there anything else that needs to be improved (I know it is a simple program, but still...)
well, can anyone enlighten me?
¿where are `Arrays::{MyArray1,MyArray2}' pointing to?


Why do I have to declare an array member like this: Array1Type* MyArray1;, using pointers. Is there any other way to define it? something like int MyArray1[10]?

¿why did you choose to use pointers? there was no problem in doing
Array1Type MyArray1[10]; //consider better names
Last edited on
Topic archived. No new replies allowed.