//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;
}
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