This is wrong for multiple reasons (one of which is returning a reference to a temporary). You need to return a reference to the data inside your class. Your class does contain an array of some kind, does it not?
'i' is your index, right? So if i==3 you would want it to return whatever value is in position [3] in your array.
That's not what you're returning here. You are returning the index itself. So if i==3, you will return 3, regardless of what is actually in your array.
What's worse, is you're returning a reference to a local variable, which your compiler probably should be yelling at you about. This is very dangerous because the reference becomes bad as soon as the function exits.
Long story short - you don't want to return i. You want to use i to figure out which element to return.
so what you guys are saying is that i should be returning something like
"return a[i];" where i variable is passed through the []operator, so it returns the element.
but how do i do that if the array name and size isnt fixed?
like i have to test others too like:
1 2 3 4 5 6 7 8 9 10 11
void test2() {
cout << "2. Array declared with two integers: IntArray b(-3, 6);" << endl << endl;
csis << "2. Array declared with two integers: IntArray b(-3, 6);" << endl << endl;
IntArray b(3, 6);
for (int i = b.low(); i <= b.high(); i++)
b[i] = i * 10;
b.setName("b");
cout << b << endl;
csis << b << endl;
wait();
]
the purpose of this assignment is being able to declare integer arrays of any size with automatic range checking of indices. and also being able to assign entire arrays to each other, compare arrays for equality/inequality, and add two arrays together.