Friend Classes

Apr 9, 2016 at 6:30am
I can't figure out what I'm doing wrong. It says I put too few arguments but I did the same thing for the add, subtract, multiply, and divide functions and they worked. Thank you for your help!
 
Last edited on Apr 9, 2016 at 7:02am
Apr 9, 2016 at 6:47am
It seems like you want the average function to average the two values in your math class, but it doesn't know what object to use.
You should change the function to this:
1
2
3
4
float average(math & mathObject)
{
    return (mathObject.n1 + mathObject.n2)/2.f;
}

declare it a friend like this:
 
friend float average(math &);

then when you call it, you should call it like this:
 
cout << "Average of " << maths.add() << " is " << average(maths);
Apr 9, 2016 at 6:58am
Thank you. But what does the & do and is it possible to do it without the & ?

Actually figured it out thank you! What you pointed out really helped!
Last edited on Apr 9, 2016 at 7:02am
Topic archived. No new replies allowed.