passing by reference

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,

thanks




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

  #include <iostream>
#include <string>

using namespace std;

void doSomething(int &x){

    x * 3;
    cout << x;

}

void getName(string name){

    cout << name;

}

int getScore(int x){

  return x;
}

int main()
{
    getScore(50);
    int x;
    x = 24;
    doSomething(getScore(&x));
    return 0;
}
  
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.

Still kind of confused so I removed the do something function and made it easier

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

#include <iostream>
#include <string>

using namespace std;


void getName(string name){

    cout << name;

}

int getScore(int &x){

  return &x;
}

int main()
{

    int x;
    x = 24;
    getScore(&x);
    return 0;
}



now in this case I'm passing by reference which I did passed the address of &x which it was expecting an address but I'm still getting an error
Line 24: Remove the &.

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.

awesome thanks that worked =) I'm going to have another look now at the original above
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.
Thanks Mikey I'm going to be in college on Friday so I'll read over them
You're welcome!
Topic archived. No new replies allowed.