Quick question regarding passing by reference. I've looked at a zillion samples of it but still can't figure out how to do this properly. Let's say I have a function and I want to use pass by reference:
double refFunction ( int &)
Ok, and then the definition:
double refFunction (int x)
{
do something here
}
Ok, now this is what all the examples I've looked at look like so I've tried to model my functions after them. When I go to compile something like this I get all kinds of errors such as: invalid initialization, error in passing argument of double refFunction, etc.
So I tried including const in front of the int & and then I only get the error: undefined reference to refFunction (int const&) so I tried switching the int and const but got the same error.
Could someone please explain to me what I am missing on all the examples I've looked at, why I'm getting errors such as these and how I properly pass by reference? Thanks!
Your example shows two different functions: double refFunction (int & x); which is declared but not defined and double refFunction (int x) - which takes different type of argument. Note there is no & in the second one, but should be.
Oooo, I figured it out....I think. If I put const before int in both the prototype and definition then it compiles. Does this sound correct or am I setting it up to mess up something else?
Well, prototype should be the same as definition, but I am pretty sure you don't want them constant an probably need to remove the keyword from both definition and declarations. Some interesting reading here: http://duramecho.com/ComputerInformation/WhyHowCppConst.html