gpt4 book ai didi

c++ - 尝试使用 *this 引用已删除的函数

转载 作者:行者123 更新时间:2023-12-05 02:30:31 24 4
gpt4 key购买 nike

我一直在为俄勒冈理工大学的 C++ 类(class)做一个项目,我遇到了一个错误,连我的教授都无法帮助我解决。我一直在尝试使用移动构造函数,但我不断收到错误提示:

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

最佳答案

Shapes(Shapes&& move) noexcept

您为此类声明了一个移动构造函数。

results in a deleted copy-assignment operator .这意味着默认情况下不提供给您:

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

...

T has a user-declared move constructor; 

...

差不多就这些了。要解决这个问题,您需要定义自己的 operator= 重载,以实现此类拥有一个重载的任何意义。

关于c++ - 尝试使用 *this 引用已删除的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71821755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com