Unable to pass by reference...

I can't pass by reference a private member via , my function... I try & but I get error. I tried making it a pointer but then I can't set it a value...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once
class ref
{
private:
	float Mage = 15;
public:
	ref();
	~ref();
	void setMage(float MageN) {
		&Mage = MageN;
	}
	float getMage() {
		return Mage;
	}
};

To enable passing by reference, all you need to do is to change the METHOD SIGNATURE. For example, you have a function:

void foo(int bar)

The bar is passed by value. Now if you want to change it to pass by reference, all you need to do is to change the signature to:

void foo(int &bar) // notice the &

There's no need to change the function body.

Now, let's talk about your program. First of all, you don't need to pass by reference here. Usually we use reference in the following 2 scenarios:

a) We're passing a custom data type (like struct or class)
b) We want to change the data

In your case, float is a built-in type, and it doesn't looks like you want to change the passed-in MageN value.

Secondly, if you insist to use reference, change the method like this:

1
2
3
void setMage(const float &MageN) {
    Mage = MageN;
}

Last edited on
This doesn't work for me for some reason. Whenever I call setMage and set it to some value for Mage it works ONLY in that same function within main? For instance I'll call getMage which does return the value I set it to in the same function. But then in a different function inside of main it doesn't instead it says the old value of 15... Why?
Line 10: This should be causing an error. The left hand side is the address of a float. The right hand side is a float. You can't assign a type float to a type address of float.

it works ONLY in that same function within main?
then in a different function inside of main it doesn't instead it says the old value of 15.

Do you mean a function call? You can't have functions within main.

Since you didn't provide main, it's difficult to guess what you're doing. It's possible you're referring to different instances of ref.



The member variable is just a regular float so when you assign MageN the value of MageN will be copied to Mage. If you want Mage to change when the variable that was passed to setMage change you probably should use a pointer instead.

float* Mage = nullptr;

1
2
3
void setMage(float& MageN) {
	Mage = &MageN;
}

Use const reference/pointer if you do not want to change the value of Mage in this class.


Not sure, is this really what you want?
Last edited on
@AbstractionAnon Yeah I put there as an example of what doesn't work but what I was thinking. And yes i mean't function calls within main. Or other function in general in my source file.

@Peter87 That'd work but I want to Mage to a default value of 15, is that possible to do and then change the value later on? TBH if there isn't I could always set it to a value in main right off the bat I guess.

Thanks for right now and in advance!
Note that you need to be careful with this. The variable that you pass to setMage must not go out of scope while you are still using the ref object or otherwise you end up with a dangling pointer.
Last edited on
Out of scope? Does that mean some kind of memory leak? Would I have to delete it or something afterwards? Sorry If I sound ignorant I'ḿ learning lol.
Not a memory leak. I show you what I mean.
1
2
3
4
5
6
7
8
9
10
11
ref r;

{

	float f = 8;
	r.setMage(f);

} // f goes out of scope here, meaning it will no longer exist.

cout << r.getMage(); // This is not safe because the variable f
                     // that r.Mage points to doesn't exist anymore. 
I'm not sure what I have shown you is really what you want. Maybe I'm just complicating things. You have named the class ref which made me think of some kind of reference class (similar to std::reference_wrapper) but maybe I'm wrong?

This allows you to do things like:
1
2
3
4
5
ref r;
float f = 8;
r.setMage(f);
f = 10;
cout << r.getMage(); // prints 10 


If you want this to print 8 instead you should probably go back to the old code that you had after your second post.
This is getting too far. Can you show us your main() or what exactly you want to implement?
Sorry for late reply! Here is what I'm trying to do. This what I have been trying to do. I know I could pass in the class name through the function as a parameter but could I just it to reference rather than that?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;
void test();
int main() {
    char response;
    ref x;
    a.setMage(10);
    test();
    cin >> response;
    return 0;
}
void test() {
    ref a;
    if (a.getMage == 0) {
        cout << "Win";
    }
    else if (a.getMage != 0) {
        cout << "Ok";
    }
}
    
}
Last edited on
x in main and a in test() are two different objects.
Yes but that's why I'm trying to pass by reference so that it's permanently chaned, unless that won't work?
If you want to pass the ref object by reference you should make it a function parameter and pass it from main.

1
2
void test(ref& a) {
    ref a;
Line 8: a is undefined. You can't reference a local variable in test() from main().

line 15,18: getMage is a function call. You need () to indicate a function call.

line 18: else if (a.getmage() != 0) is unnecessary. All you need is else. If it's not 0, then it can only be non-zero.

Line 23: extra }

but could I just it to reference rather than that?

Sorry, that sentence makes no sense. I don't know what you mean.

As Peter87 suggest, pass ref by reference.
I think this is what you want:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

void test (ref &);    // added pass by reference to prototype

int main () 
{  char response;
    ref x;
    x.setMage(10);  // changed from a to x
    test (x);             // pass x by reference
    cin >> response;
    return 0;
}

void test (ref & a)   // added argument as pass by reference
{  if (a.getMage() == 0)   // added ()
    {  cout << "Win";
    }
    else                             // removed if
    {  cout << "Ok";
    }
}


Oh my goodness thanks, I'm sorry about that sentence I must have been drowsy or something but thank you!
Topic archived. No new replies allowed.