You'd be surprised how much programming and math are related! =)
In fact, before I wanted to program I pursued a math major with more of a background in creative writing. I don't know how but I managed to discover programming and realized it gave me the best of both worlds, so now I devote my time to studying Programming!
Anyways, back to your initial problem. Before explaining how the trick is done, it might be more useful to explain what is going on in your program.
Whenever you declare a storage type with an identifier (or anonymously without an identifier). without using new, you are using memory on the stack to store data that your program is working with.
Because your data types exist somewhere in memory, they must be referenceable or have some means of being located and manipulated. That is the idea of a reference - a memory address.
So when you pass something in a method by reference, you are passing a handle to the actual type that sits on top of the address.
Let's look at the following example--
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
void make5(int& ref){
ref = 5;
}
int main(){
int x = 9; // x exists on the stack, and is constructed with value 9
cout << x << endl; // printing out x
make5(x); // passing x into the method by reference
cout << x << endl; // x should be 5 now
cin.get();
return 0;
}
--notice that x is passed to the method by reference. The address of the object itself is passed and then it is assigned with the value of 5, then x is printed again. Notice that there is no direct assignment to x itself, rather the method simply did as it was instructed and assigned a value to whatever int reference was passed to it (it could have been anything, y even (as an int) ).
Your assignment needs to have the same logic. You must have a referenceable type passed into the method, then assigned the result.