Error C2679: Binary '<<': Operator not found that accepts a right-hand operand of type Rectangle (or no acceptable conversion)

I need to write a function to overload the == operator to compare width, height and color. I need to return "Y" if its equal and "N" if it isn't.

This is my code, which I believe is correct, but keeps giving me an error:

error C2679: binary '<<: no operator found that accepts a right-hand operand of type "Rectangle" (or no acceptable conversion)

I searched for an answer and nothing came close to comparing three data as most of the examples are for comparing two data.

#include <iostream>
#include <string>
using namespace std;

class Rectangle
{
private:
    float width;
    float height;
    char colour;
public:
    Rectangle()
    {
        width=2;
        height=1;
        colour='Y';
    }
    ~Rectangle(){}
    float getWidth() { return width; }
    float getHeight() { return height; }
    char getColour() { return colour; }

    Rectangle(float newWidth, float newHeight, char newColour)
    {
        width = newWidth;
        height = newHeight;
        colour = newColour;
    }

    char operator== (const Rectangle& p1){

        if ((width==p1.width) && (height==p1.height) && (colour==p1.colour))
            return 'Y';
        else
            return 'N';
    }
};

int main(int argc, char* argv[])
{
    Rectangle rectA;
    Rectangle rectB(1,2,'R');
    Rectangle rectC(3,4,'B');
    cout << "width and height of rectangle A is := " << rectA.getWidth() << ", " << rectA.getHeight() << endl;
    cout << "Are B and C equal? Ans: " << rectB==rectC << endl;


    return 0;
}

      

+2


source share


2 answers


"<<" takes precedence over "==". Put your comparison in parentheses:



cout << "Are B and C equal? Ans: " << (rectB == rectC) << endl;

      

+8


source


It looks like you need parentheses:

cout << "Are B and C equal? Ans: " << (rectB==rectC) << endl;

      



This is an operator priority issue; <<

applies to rectB

pre-launch ==

.

+3


source







All Articles