Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C++
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?

ostream &operator<<( ostream &output, const Yup &num )
    return output;
Yup &operator+(const Yup &tr1,const Yup &tr2 )
    Yup k(0);
    return k;
Posted 25-Nov-12 2:29am
Edited 25-Nov-12 3:50am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Your operator+ overload[^] returns a reference to a local variable.
Remove the reference:
Yup operator+( const Yup &tr1,const Yup &tr2 )
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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

  Print Answers RSS
0 OriginalGriff 235
1 Sergey Alexandrovich Kryukov 120
2 Richard MacCutchan 100
3 ChauhanAjay 95
4 BotCar 95
0 Sergey Alexandrovich Kryukov 9,056
1 OriginalGriff 8,041
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928

Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 25 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100