C++ Templates

I just started getting into templates.I am trying to code this so it supports all data types instead of just int. i know what i did is wrong for sure. please show me how to do this. thanks

1
2
3
4
5
6
7
8
9
10


T & myArray < T >::operator[](int index) {
    if (index >= start && index <= end) {
	return (array[index - start]);
    }
    cout << "Error: Index out of bound" << endl;
    exit(1);
}
Last edited on
1
2
3
4
5
6
7
8
template <typename T>
T & myArray < T >::operator[](int index) {
    if (index >= start && index <= end) {
	return (array[index - start]);
    }
    cout << "Error: Index out of bound" << endl;
    exit(1);
}
Sorry but is this the right syntex?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    myArray <int> myList(2, 13);
    myArray <int>yourList(-5, 9);
    myArray <int> i;

    cout << "Elements in myList: ";
    for (i = 2; i < 13; i++)
	cout << myList[i] << " ";
    cout << endl;
    cout << "Elements in yourList: ";
    for (i = -5; i < 9; i++)
	cout << yourList[i] << " ";
    cout << endl;
    return 0;
}
Does it compile?
Topic archived. No new replies allowed.