Ask your own question, for FREE!
Computer Science 7 Online
OpenStudy (aravindg):

help

OpenStudy (aravindg):

can u identify error in my program below its not working either #include<iostream.h> #include<conio.h> class distance { int m,cm; public: void read(); void operator +(distance); void display(); }; void distance::read() { cout<<"\n\nEnter distances:"; cin>>m>>cm; } void distance::display() { cout<<"\n distances:"; cout<<m<<cm; } void distance::operator +(distance d1) { distance temp; temp.cm=cm+d1.cm; temp.m=m+d1.m; if(temp.cm>=100) { temp.m+=1; temp.cm=(temp.cm%100); } } void main() { distance d3,d4,d5; d3.read(); d4.read(); d5=d3+d4; d5.display(); getch(); }

OpenStudy (aravindg):

@slotema

OpenStudy (anonymous):

There's problem with the operator+ method. When you do the assignment `d5 = d3 + d4;`, it'll be translated into something like `d5 = d3.operator+(d4);`. But operator+ returns a void! So you're trying to assign a void to a distance, which won't work unless you overload operator= to work on voids. The best way to fix this problem is to have operator+ return a new distance with the new m and cm values.

OpenStudy (aravindg):

oh thanks a lot for the spot....that was a silly but serious mistake @Opcode u must see this

OpenStudy (anonymous):

No problem. And most serious mistakes are caused by some small, seemingly unimportant detail.

OpenStudy (aravindg):

:)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!