#include <iostream> using namespace std; class Cube { int w,l,d; public: Cube(int w, int l, int d) : w(w), l(l), d(d){} int getWidth() const {return w;} int getLength() const {return l;} int getDepth() const {return d;} }; ostream& operator<< (ostream&os, const Cube& c) { os << "( " << c.getWidth() << ", " << c.getLength() << ", " << c.getDepth() << ")"; return os; } Cube operator+ (Cube& c1, Cube& c2) { int n = c1.getWidth() * c1.getDepth() * c1.getLength(); int d = c2.getWidth() * c2.getDepth() * c2.getLength(); int t = n + d; return Cube(t); } int main() { Cube c1(3,5,9), c2(2,1,4); cout << c1 << endl; cout << c2 << endl; cout << "Total Volume: " << c1 + c2; } In my operator+ there are some mistakes which I couldn't find out. The operator overloading for ( + ) should add up two cubes which will result in summing up of the volume of both cubes.
What should I do to my operator overloading for ( + ) ?
Cube(t), so I guess the "mistake" is you forgot to write one?return Cube(t);What are you returning in this case? You'rconstructoris of the formCube(int, int, int)Cubeactually has. The one with 3 arguments.