I want to get a sum of 2 objects

I get an error when I try to add two objects like
Box3 = Box1 + Box2;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  #include <iostream>
using namespace std;

class Box {
   public:
      double getVolume(void) {
         return length * breadth * height;
      }
      void setLength( double len ) {
         length = len;
      }
      void setBreadth( double bre ) {
         breadth = bre;
      }
      void setHeight( double hei ) {
         height = hei;
      }

      // Overload + operator to add two Box objects.
      Box operator+(const Box& b) {
         Box box;
         box.length = this->length + b.length;
         box.breadth = this->breadth + b.breadth;
         box.height = this->height + b.height;
         return box;
      }

   private:
      double length;      // Length of a box
      double breadth;     // Breadth of a box
      double height;      // Height of a box
};

// Main function for the program
int main() {
   Box Box1;                // Declare Box1 of type Box
   Box Box2;                // Declare Box2 of type Box
   Box Box3;
                  // Declare Box3 of type Box
   double volume = 0.0;     // Store the volume of a box here

   // box 1 specification
   Box1.setLength(6.0);
   Box1.setBreadth(7.0);
   Box1.setHeight(5.0);

   // box 2 specification
   Box2.setLength(12.0);
   Box2.setBreadth(13.0);
   Box2.setHeight(10.0);

   // volume of box 1
   volume = Box1.getVolume();
   cout << "Volume of Box1 : " << volume <<endl;

   // volume of box 2
   volume = Box2.getVolume();
   cout << "Volume of Box2 : " << volume <<endl;

   // Add two object as follows:
   Box3 = Box1 + Box2;
    Box3.getVolume();
   // volume of box 3
   volume = Box3.getVolume();
   cout << "Volume of Box3 : " << volume <<endl;
   cout << "The sum of Box1 & Box2 = " << (Box3 = (Box1 + Box2)) << endl;

   return 0;
}

I get this error:
C:\Martin\MalikChapter3\overloadOperatorPlus.cpp: In function 'int main()':
C:\Martin\MalikChapter3\overloadOperatorPlus.cpp:66:64: error: no match for 'operator<<' in 'std::operator<< <std::char_traits<char> >((* & std::cout), ((const char*)"The sum of Box1 & Box2 = ")) << (Box3 = (*(const Box*)(& Box1.Box::operator+((*(const Box*)(& Box2))))))'

Please help
Hi,

Logically what does it mean to add boxes? Presumably the only sensible thing would be to add the volumes. The operator+ isn't doing that.

You also need to overload operator<< so the compiler knows what to do when you want to print a Box. This is the error on line 66.

Good Luck !!
1
2
3
4
5
6
      Box operator+(const Box& b) {
         length += b.length;
         breadth += b.breadth;
         height += b.height;
         return box;
      }


In the expression Obj1 + Obj2,
Obj1 is the one that calls the operator. So length, breadth and height would refer to the length breadth and height of the calling object which is Obj1. Second operand is captured into 'b'.

I think you only use this-> when local variable has the same name. Somebody else can explain this->'s use better.
On line 60 & 61 there is that code:
// Add two object as follows:
Box3 = Box1 + Box2;

I changed line 66 to this:
1
2
cout << "The sum of Box1 & Box2 = ";
    Box3 = (Box1 + Box2) ;

and I now get this output:
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400
The sum of Box1 & Box2 =
Process returned 0 (0x0) execution time : 0.131 s
Press any key to continue.

The sum of the two volumes (210 + 1560) is not displayed.

Please help
You should be printing volume not Box3 = (Box1 + Box2)

1
2
3
   volume = Box3.getVolume();
   cout << "Volume of Box3 : " << volume <<endl;
cout << "The sum of Box1 & Box2 = " << volume << endl;
Bopaki wrote:
The sum of the two volumes (210 + 1560) is not displayed.



TheIdeasMan wrote:
Logically what does it mean to add boxes? Presumably the only sensible thing would be to add the volumes. The operator+ isn't doing that.


If I have 2 boxes, each being 1 by 1 by 1, and I add them by your method, I get a volume of 8, not 2.
It does not make much sense to add two boxes this way.
Nevertheless, to illustrate the canonical implementation of this overloaded operator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

struct box {

    box() = default ;
    box( double l, double w, double h ) : length(l), width(w), height(h) {

        if( length <= 0 || width <= 0 || height <= 0 ) throw "invalid argument" ;
    }

    // ...

    box& operator+= ( const box& that ) {

        length += that.length ;
        width += that.width ;
        height += that.height ;
        return *this ;
    }

    private:
        double length = 1 ;
        double width = 1 ;
        double height = 1 ;

    // https://en.cppreference.com/w/cpp/language/operators#Binary_arithmetic_operators
    friend box operator+ ( box a, const box& b ) { return a += b ; }
    
    // https://en.cppreference.com/w/cpp/language/operators#Stream_extraction_and_insertion 
    friend std::ostream& operator<< ( std::ostream& stm, const box& b ) {

        return stm << "box{" << b.length << 'x' << b.width << 'x' << b.height << '}' ;
    }
};

int main() {

    const box a( 12, 34, 56 ) ;
    const box b( 78, 90, 12 ) ;
    std::cout << a << " + " << b << " == " << a+b << '\n' ;
}

http://coliru.stacked-crooked.com/a/c9eb5baff589159a
There are two examples of operator+ in the tutorial:

http://www.cplusplus.com/doc/tutorial/templates/

But your situation is logically different.
I here what you say TheIdeasMan but the sum of Box1 & Box2 = 1770 that is: 210 + 1560
that is what line 60 & 61 says.
When you have a sphere, you increase its dimension by increasing its radius, not by increasing its volume.
When you have cuboid, you increase its dimensions by increasing its length, width and height, not by increasing its volume because then you can't trace back its length, width and height.

So technically in this case you're not adding to cuboids. You're increasing a cuboid's dimension by the magnitude of the dimensions of another cuboid.

If you had to do it with volumes then you wouldn't need an operator for +. And you would not know the dimensions of the new cuboid because you've lost them when you added the volumes. If you had to do it by extending the dimensions manually by adding to length, breadth and height then again you wouldn't need operator for + and it would be more cumbersome.

Addition of two separate cuboids has nothing to do with their dimensions. It only has to do with their volume. So again you wouldn't need an operator for +.

Anyways it's just a problem nothing of that really matters.
Last edited on
Thanks to everybody who assisted me.
I got the logic of the situation.

Enjoy the weekend!!!
Topic archived. No new replies allowed.