overloading question

Trying to make this overloaded function work -- only successful by adding another return field to 2 in the return statement.

from

1
2
3
4
Fraction operator*(Fraction &f1, int val)
		{
			return Fraction(f1.getValNum()* val) // 1 here wont run
		}


1
2
3
4
Fraction operator*(Fraction &f1, int val)
		{
			return Fraction(f1.getValNum()* val, f1.getValDenom()); //add one it will
		}


First I thought you could only return one value in a function.?
Two, why did I have two add the second field to make it work?
First I thought you could only return one value in a function.?

That's correct, you can only return one value from a function. In this case that one value is a Fraction.

Two, why did I have two add the second field to make it work?

Probably because you don't have a Fraction constructor that takes one integer.

Do you realize that you're creating a "new" instance of a Fraction in that function?

@OP: In case it's not already clear, in each of those code snippets, you are only returning one value. When you "added the second field", you weren't adding a second value that's being returned. You added a second argument to the constructor of the single object that's being returned.
Topic archived. No new replies allowed.