hi guys,how come when passing &x I get an error I thought that since I specified I would be passing in an address of a variable but I get an error invalid conversion from 'int* to int
how would I solve this,it's probably something small I'm missing,
Line 7: doSomething is expecting an int to be passed by reference.
Line 20: getScore() expects one argument by value.
Line 21: Returns an int (i.e. a value, not a reference)
Line 30: Couple of things wrong here:
1) You're passing the address of x (int *) to getScore which is expecting a simple int.
2) getScore() returns a simple int value, which you then try to pass to doSomething() which is expecting an int by reference. You can't pass an int value to a function expecting an int reference.
When you pass by reference, you don't have to take the address of the argument. Just pass x.
The compiler knows from the function definition (line 14) to pass by reference.
Line 16: Remove the &.
You don't want to return the address of x. You simply want to return x.
I think the problem might be because you're confusing two different ways we use the word "reference".
When we say "pass by reference", we're talking about a concept - that the thing we pass into a function is something that allows the function to refer back to the object in the calling code, rather than a local copy of that object. This can be achieved in different ways. In some languages, everything is passed by reference automatically, so there's no need to do anything special in the code. In C, we could do this by passing a pointer into the function, rather than the object itself.
There is also a specific thing in the C++ language called a "reference" - and this is what you've used in line 14, in the definition of the argument to getScore. A C++ reference is not the same as a pointer, so when you try and pass a pointer into the function, as you do at line 30, then the compiler quite rightly tells you that you're passing in an argument of the wrong type.
If you haven't understood what references are, or how they differ from pointers, I'm sure your textbook will have a section about them.