Help with Error 'cannot bind'

I keep getting this error:
cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
using namespace std;

class Square {
	
   private:
      double side;
     
      
   public:

      double getArea() {
         return side * side;
      }
		
      void setSide( double s ) {
         side = s;
      }

     
     
		
      // Overload + operator to add two Square objects.
      Square operator+(const Square& b) {
         Square square;
         square.side = side + b.side;
         return square;
      }
      
        Square operator-(const Square& b) {
         Square square;
         square.side = side - b.side;
         return square;
    }
        
        
         Square operator*(const Square& b) {
         Square square;
         square.side = side * b.side;
         return square;
         
         }
         
         Square operator/(const Square& b) {
         Square square;
         square.side = side / b.side;
         return square;
      }
    };

// Main function
int main( ) {
   Square Square1;                
   Square Square2;                
   Square Square3; 
   
   double area = 0.0;     
 
   
   Square1.setSide(2.0);
   
   Square2.setSide(5.0);
 
   
   // Square1 area
   area = Square1.getArea();
   cout << "Area of Square1 : " << area <<endl;
 
   // Square2 area
   area = Square2.getArea();
   
   cout << "Area of Square2 : " << area <<endl;

      char op;
  
    cout << "Enter the operator: ";
    cin >> op;
    switch (op)
    {
    case '+':
        Square3 = Square1 + Square2;
        cout << Square3;
        break;
    case '-':
        Square3 = Square1 - Square2;
        cout << Square3;
        break;
    case '*':
        Square3 = Square1 * Square2;
        cout << Square3;
        break;
    default:
        cout << "No a valid operator";
    }
   return 0;
}
The operator << hasn't been defined for a Square object (lines 82 etc.)

Either define it within the Square class, or cout whatever specific things about Square3 you actually intended (its area, maybe?)
Last edited on
Ok, so how would I Display Square3. I want to display the new value of the square from for example Square1 + Square2 which is in the first case. How would I define it in Square?
Square is a type of object. What exactly are you trying to display about Square3? Its side length? Its area?

If you want to write out its area then just use
cout << Square3.getArea();
in lines 82, 86, 90.
I guess this is what you are asking for:
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
using namespace std;

class Square {
        
   private:
      double side;
     
      
   public:

      double getArea() {
         return side * side;
      }
                
      void setSide( double s ) {
         side = s;
      }

     
     
                
      // Overload + operator to add two Square objects.
      Square operator+(const Square& b) {
         Square square;
         square.side = side + b.side;
         return square;
      }
      
        Square operator-(const Square& b) {
         Square square;
         square.side = side - b.side;
         return square;
    }
        
        
         Square operator*(const Square& b) {
         Square square;
         square.side = side * b.side;
         return square;
         
         }
         
         Square operator/(const Square& b) {
         Square square;
         square.side = side / b.side;
         return square;
      }


      // This line needed to get at any private variables for a Square (e.g. side)
      friend ostream &operator<<( ostream &stream, Square sq );
    };


// An operator to << a square
ostream &operator<<( ostream &stream, Square sq )
{
   stream << "The user still hasn't said what he wants, so ...\n";
   stream << "... HERE'S A SQUARE!!!\n";
   stream << endl;
   stream << "       *******\n";
   stream << "       *     *\n";
   stream << "       *     *  " << "Side length: " << sq.side      << endl;
   stream << "       *     *\n";
   stream << "       *     *  " << "Area       : " << sq.getArea() << endl;
   stream << "       *     *\n";
   stream << "       *******\n";
   stream << endl;

   return stream;
}



// Main function
int main( ) {
   Square Square1;                
   Square Square2;                
   Square Square3; 
   
   double area = 0.0;     
 
   
   Square1.setSide(2.0);
   
   Square2.setSide(5.0);
 
   
   // Square1 area
   area = Square1.getArea();
   cout << "Area of Square1 : " << area <<endl;
 
   // Square2 area
   area = Square2.getArea();
   
   cout << "Area of Square2 : " << area <<endl;

      char op;
  
    cout << "Enter the operator: ";
    cin >> op;
    switch (op)
    {
    case '+':
        Square3 = Square1 + Square2;
        cout << Square3;
        break;
    case '-':
        Square3 = Square1 - Square2;
        cout << Square3;
        break;
    case '*':
        Square3 = Square1 * Square2;
        cout << Square3;
        break;
    default:
        cout << "No a valid operator";
    }
   return 0;
}
Topic archived. No new replies allowed.