I have an abstract class called Shape
and am trying to store two Rectangle
objects. However, when I try to assign Rectangle
to a Shape
object, the compiler flags me an error about the assignment operator.
I am confused as to why this gives an error
void createTwoRectangles(Shape* shape, int size) {
Rectangle* r1 = nullptr;
Rectangle* r2 = nullptr;
shape[0] = createRectangle(&r1);
shape[1] = createRectangle(&r2);
}
But this is fine
void createTwoRectangles(Shape* shape, int size) {
Rectangle* r1 = nullptr;
Rectangle* r2 = nullptr;
createRectangle(&r1);
createRectangle(&r2);
shape[0] = *r1;
shape[1] = *r2;
}
Correct me if I am wrong, but I am still assigning a rectangle object into shape? Why is the compiler saying that no operator matches these operands on the first function, but doesn't in the second?
Here is my overloaded =
operator for the Rectangle
class
Rectangle& Rectangle::operator=(const Rectangle& rectangle) {
if (this != &rectangle) {
lowerLeft = rectangle.ll;
upperRight = rectangle.ur;
}
return *this;
}
And if I overloaded the assignment operator wrong, how can I overload it correctly to work with Shape objects?
EDIT -- This is my createRectangle
function
Rectangle** createRectangle(Rectangle** rec) {
Point* lowerPoint = nullptr;
Point* upperPoint = nullptr;
cout << "\n Lower left point -\n";
createAPoint(&lowerPoint);
cout << "\n Upper right point -\n";
createAPoint(&upperPoint);
while (*upperPoint < *lowerPoint) {
cout << "\n Error! Upper right point must be greater than lower left.\n";
cout << " Upper right point - \n";
createAPoint(&upperPoint);
}
if (*rec)
(*rec)->update(*upperPoint, *lowerPoint);
else
*rec = new Rectangle(*upperPoint, *lowerPoint);
delete lowerPoint;
delete upperPoint;
return rec;
}