Overload stream operator.

Having trouble figuring out how to overload the stream operator for this case in my assignment. What am I'm doing incorrectly?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//OVERLOADING THE INSERTION OPERATOR - PRINTING A RECTANGLE OBJECT
		case 4:
		{
			//Declarations for Case 4:
			Rectangle r1(10, 5);

			//In the appropriate file and location: 
			//Write the function to overload the stream insertion operator that will print out a rectangle's dimensions.
			//Note that rectangle r1 has already been declared and initialized for you above.
			//The output should appear EXACTLY like  this:  

			//Rectangle dimensions:
			//		Length:		10
			//		Width:		 5

			//Once you have written the code to implement the overloaded insertion operator, uncomment the line of code below that calls it.
				/**************************************************************************************************************/
			cout << r1 << endl;
				/**************************************************************************************************************/

				break;
			}

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
// Rectangle.cpp
#include "Rectangle.h"
#include <string>

using namespace std;

Rectangle::Rectangle(int length, int width)
{
	setLength(length);
	setWidth(width);
}

int Rectangle::getLength()
{
	return length;
}
int Rectangle::getWidth()
{
	return width;
}
void Rectangle::setLength(int l)
{
	if (l > 0)
		length = l;
}
void Rectangle::setWidth(int w)
{
	if (w > 0)
		width = w;
}

// determine if two Rectangles are equal and
// return true; otherwise, return false
bool Rectangle::operator==(const Rectangle& right) const {
	bool isEqual{ false };
	if ((length == right.length && width == right.width) || (length == right.width && width == right.length))
		isEqual = true;

	return isEqual;	
}

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
//Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <iostream>

class Rectangle
{
	friend std::ostream& operator <<(ostream&, Rectangle& r1);

public:
	
	Rectangle(int, int);
	int getLength();
	int getWidth();
	void setLength(int);
	void setWidth(int);
	bool operator==(const Rectangle&) const; // equality operator

private:
	int length{ 0 };
	int width{ 0 };

};

std::ostream& operator <<(ostream &output, Rectangle& r1)
{

	output << "Length: " << r1.setLength << endl;
	output << "Width: " << r1.setWidth << endl;
	return output;
}

#endif 




Last edited on
1
2
	output << "Length: " << r1.setLength << endl;
	output << "Width: " << r1.setWidth << endl;


Well, setLength and setWidth are
(1) functions - so you need ( ) notation;
(2) probably the wrong functions - at a rough guess you meant getLength() and getWidth().

You haven't given enough code to risk a definitive answer, but you could try
output << "Length: " << r1.getLength() << '\n';
output << "Width: " << r1.getWidth() << '\n';
> What am I'm doing incorrectly?
in operator<<
- ignored the "Rectangle dimensions:" in the output
- didn't indent Length and Width
- defined a non-inline function in a header
- forgot the std:: specifier
- asked for a non-const reference to Rectangle
- declared a function as friend when you already provide accessors
- tried to call setters (¿?)

also
- failed to declare `getLength()' and `getWidth()' as const
In "Rectangle.cpp", not "Rectangle.h":

1
2
3
4
5
6
7
std::ostream& operator <<(std::ostream& output, Rectangle& r1)
{
   output << "Length: " << r1.getLength() << '\n';
   output << "Width: " << r1.getWidth() << '\n';

   return output;
}

(Move #include <iostream> from your header to your source file, preferably before your custom header include)

You were not calling functions in operator<< If you had added () you'd call the wrong functions.

Using a simplified driver program that compiles and uses the class feature you are working on:

1
2
3
4
5
6
7
8
9
10
#include <iostream>

#include "Rectangle.h"

int main()
{
   Rectangle r(4, 5);

   std::cout << r << '\n';
}

Length: 4
Width: 5

Personally I would make operator<< public, not private. Not that it makes a difference with Visual Studio 2019.

using namespace std;. Don't use it.

Make your getter methods const, so the compiler catches any attempt to alter the member data:

13
14
	int getLength() const;
	int getWidth() const;

Add const to your method definitions in the source file.
Topic archived. No new replies allowed.