### Implicit conversion trouble

Why aren't this codes the same?
 ``1234567891011121314`` ``````inline Fract operator+(const Fract& f1,const Fract& f2){ Fract rez; return rez.add(f1,f2); } //.... Fract Fract::add(const Fract& f1,const Fract& f2) { int lcmb=gcf_or_lcm(f1.den,f2.den,2); int mult1=lcmb/f1.den; int mult2=lcmb/f2.den; Fract rez(f1.num*mult1+f2.num*mult2,lcmb); return rez; //here }``````

 ``12345678910111213`` ``````inline Fract operator+(const Fract& f1,const Fract& f2){ Fract rez; return rez.add(f1,f2); } //... Fract Fract::add(const Fract& f1,const Fract& f2) { int lcmb=gcf_or_lcm(f1.den,f2.den,2); int mult1=lcmb/f1.den; int mult2=lcmb/f2.den; return (f1.num*mult1+f2.num*mult2,lcmb); //and here }``````

The "num" and "den" variables are of type int.You can see the difference at line 11
I mention that the class "Fract" has got a constructor that takes two ints.
But anyway,the result is different,where is the problem ?
...any idea ?
 ,where is the problem ?

Right here:

`return (f1.num*mult1+f2.num*mult2,lcmb); //and here `

First you can only return one item from a function, not two.

Second what exactly do you want to return from this function, what type of variable?
Interesting, I didn't know you could use the comma operator like that. I definitely don't recommend doing it the second way as I'm sure a lot of people are going to read that and not have a clue what you just did. The first way at least makes sense as to what you're going after.

Anyways, how are they different?

EDIT:
jlb wrote:
First you can only return one item from a function, not two.

Actually, from what I just read this is actually legal.
http://stackoverflow.com/questions/2539458/c-return-x-y-what-is-the-point
Last edited on
It may be legal but it is still only returning one item, and I guarantee it is not what the OP is trying to accomplish.

When you return an item from a function it must be the same type of variable as in the function signature. In this case a Fract. Do you see this return statement returning a Frac?
I believe what's it doing is creating a Fract object implicitly, using the values in the return statement for the constructor.

I could be entirely wrong here though, I've never seen this before.
 using the values in the return statement for the constructor.

What constructor? I don't see the use of any constructor in that return statement.
The function returns a Fract object. I think you're missing the implicit word I keep using. ie, you're not going to see it in the code. The compiler does it in the background.

EDIT:
After further reading, I believe I am wrong here. Although it would be interesting behavior.
Last edited on
No I'm not missing the implicit word. The compiler will not do it in the background, you must return the proper type.

ok...so simplifying my code i realized that actually the one int constructor is called there...i was sure that Frameworks respond on this topic was corect http://www.cplusplus.com/forum/beginner/94383/ ... he does exactly what i tried to do but now i realize that there are some mistakes.
Last edited on
Topic archived. No new replies allowed.