Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempting to reference a deleted function using *this

Tags:

c++

I have been working on a project for a C++ course at Oregon Tech, and I have had trouble with an error that even my professor hasn't been able to help me with. I have been trying to use a move constructor, but I keep getting errors saying:

function "Shapes::operator=(const Shapes &)" (declared implicitly) cannot be referenced -- it is a deleted function - line 31

'Shapes::operator=(const Shapes &)': attempting to reference a deleted function - line 31

// Shapes.h

#ifndef LAB1_SHAPES_H
#define LAB1_SHAPES_H

class Shapes {
protected:
    float m_width;
    float m_area;
    float m_perimeter;
public:
    Shapes() {
        m_width = 0;
        m_area = m_width * m_width;
        m_perimeter = 4 * m_width;
    }

    Shapes(float x) {
        if (x > 0) {
            m_width = x;
        }

        m_area = m_width * m_width;
        m_perimeter = 4 * m_width;
    }

    Shapes(Shapes&& move) noexcept {
        *this = move;
    }

    Shapes(const Shapes& copy) {
        m_width = copy.m_width;
        m_area = m_width * m_width;
        m_perimeter = 4 * m_width;
    }

    float getWidth() const {
        return m_width;
    }
    float getArea() const {
        return m_area;
    }
    float getPerimeter() const {
        return m_perimeter;
    }
};

#endif //LAB1_SHAPES_H
like image 547
Cameron McHatton Avatar asked Jan 25 '26 13:01

Cameron McHatton


1 Answers

Shapes(Shapes&& move) noexcept

You declared a move constructor for this class.

This results in a deleted copy-assignment operator. This means that one is not provided to you by default:

An implicitly-declared copy assignment operator for class T is defined as deleted if any of the following is true:

...

T has a user-declared move constructor; 

...

That's pretty much it. To work around it you'll need to define your own operator= overload, to do whatever it means for this class to have one.

like image 163
Sam Varshavchik Avatar answered Jan 28 '26 03:01

Sam Varshavchik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!