0

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;
}
Jon Quang
  • 9
  • 1
  • How is `createRectangle` declared? – cadaniluk Dec 06 '15 at 17:28
  • Your assignment is fine, even though checking for self-assignment should be avoided. What does your `createRectangle` function return? – DeiDei Dec 06 '15 at 17:30
  • What does assigning a shape to a rectangle mean? A shape may not be a rectangle. – Neil Kirk Dec 06 '15 at 17:31
  • You mean assign a rectangle to a shape, right? – juanchopanza Dec 06 '15 at 17:32
  • @juanchopanza Sorry for the mistake, and yes that is correct. I updated my post to include the `createRectangle` function as well. – Jon Quang Dec 06 '15 at 17:35
  • Possible duplicate of [What is object slicing?](http://stackoverflow.com/questions/274626/what-is-object-slicing) – Sneftel Dec 06 '15 at 17:36
  • Your `createRectangle` returns a pointer to a pointer to a `Rectangle`. Is that what your `shape[i]` requires? Or is it a plain pointer to `Rectangle`? – DeiDei Dec 06 '15 at 17:36
  • @DeiDei `shape` is a `Shape*`, so `shape[n]` is a `Shape`. But that is just one of many problems. The code is just too broken. – juanchopanza Dec 06 '15 at 17:39
  • This code is a complete mess. Stop using pointers to implement pass-by-reference ; use references instead. Stop using pointers to indicate ownership. Avoid redundant return values like `createRectangle` has. And post a [MCVE](http://stackoverflow.com/help/mcve). Although it's obvious what is causing your compiler error (trying to assign `Rectangle **` to `Shape`) it's not possible to suggest a fix based on what you have posted so far. The whole design needs an overhaul. – M.M Dec 06 '15 at 21:10

0 Answers0