Operator Overload

Hello world, first time poster here.

I'm having some trouble using Operator Overloading in a C++ project I'm working on. I've read articles and tried to understand, but I'm just not able to get my code to do what I'm trying to do.

Basically I have a header called sphere.h, in this header I have a class called sphere. This class is used to create spheres, calculate volume, etc. I successfully overloaded an operator so that I can say "sph1 = sph2" and it assigns the center coords. and radius from sph2 into sph1.

Now, I'm trying to make one so I can say "sph# = sph1+sph2" and it will apply it will add the coords. and radius together and store them in a new sphere.

Below are pastebins of my code:
sphere.h header file: http://pastebin.com/7m0q7sNu
sphere.c++ source: http://pastebin.com/6qs6ubJi

My code currently compiles, but I can't figure out how to use sph1 and sph2, and then assign them to sph#.

Any help would be much appreciated.

Thanks,
-Justin B.
1
2
//void operator +(const sphere &x);
sphere operator +(const sphere &x) const;
The result of the operation is an sphere and you can operate with constant objects
I tried this, but now I'm getting a bunch of errors. :(
Change the return value of operator+ to sphere. You should have it return const but your code wont work with it until you you use a temp sphere ie temp = this; You also need to return *this; in op+ function and test in main
1
2
cout << "Adding sph1 and sph2\n" << endl;
	sph3 = sph1 + sph2;
Topic archived. No new replies allowed.