Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey

I have a class named Yup and it has one data member of type int(called number).
I have overloaded the cout and + operators.

cout<<object(type Yup)==> prints the number.
The object1+object2 adds the numbers of 2 objects.

In the main I have

Yup t(23),t1(27);

When I do this: Yup t2=t+t1; cout<<t2<<endl;==> I get 50;


When I do this: cout<<(t+t1)<<endl; ==> I get -87293...

How can i write it in one line?
How it is compiled?


C#
ostream &operator<<( ostream &output, const Yup &num )
{
    output<<num.number;
    return output;
}

Yup &operator+(const Yup &tr1,const Yup &tr2 )
{
    Yup k(0);
    k.number=tr1.number+tr2.number;
    return k;
}
Posted
Updated 25-Nov-12 3:50am
v3
Comments
[no name] 25-Nov-12 9:21am    
show your opeator+ overloading code

Your operator+ overload[^] returns a reference to a local variable.

Remove the reference:
C++
Yup operator+( const Yup &tr1,const Yup &tr2 )
 
Share this answer
 
You probably have defined operator+ incorrectly. It should return an object of your class that contains the result. If in doubt, show your code by using the "Improve question" button on your question.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900