问题描述
我正在尝试解决将我的构建系统操作系统和 gcc 版本升级到 9 所产生的错误。我可以用以下代码进行演示。
class Cl {
private:
float f;
public:
constexpr float GetF() const { return f; }
Cl& operator=(const Cl& other) {
f = other.GetF();
return *this;
}
Cl& operator=(const float& other) {
this->f = other;
return *this;
}
explicit constexpr Cl(const float& val) : f(val) {}
};
struct Sl {
float x,y;
Cl lcl;
constexpr Sl(const float &init_x,const float &init_y,const Cl &cl) : x(init_x),y(init_y),lcl(cl) {}
};
typedef struct Sl sdata;
int main()
{
const float fx = 30.30;
Cl c1(fx);
sdata s1(0,0.0,c1);
return 0;
}
编译:
preetam@preetam-Precision-M4800 ~ $ g++-9 -Werror=deprecated-copy dp_test.cc
dp_test.cc: In constructor ‘constexpr Sl::Sl(const float&,const float&,const Cl&)’:
dp_test.cc:22:101: error: implicitly-declared ‘constexpr Cl::Cl(const Cl&)’ is deprecated [-Werror=deprecated-copy]
22 | constexpr Sl(const float &init_x,lcl(cl) {}
| ^
dp_test.cc:8:6: note: because ‘Cl’ has user-provided ‘Cl& Cl::operator=(const Cl&)’
8 | Cl& operator=(const Cl& other) {
| ^~~~~~~~
cc1plus: some warnings being treated as errors`
错误的含义是什么?
constexpr Cl(const Cl& other) {
f = other.GetF();
}
它导致了错误:
preetam@preetam-Precision-M4800 ~ $ g++-9 -Werror=deprecated-copy dp_test.cc
dp_test.cc: In copy constructor ‘constexpr Cl::Cl(const Cl&)’:
dp_test.cc:10:9: error: member ‘Cl::f’ must be initialized by mem-initializer in ‘constexpr’ constructor
10 | }
| ^
dp_test.cc:5:9: note: declared here
5 | float f;
| ^
最终通过以下复制构造函数修复:
constexpr Cl(const Cl& other) : f(other.f) {}
解决方法
lcl
结构体的 Sl
成员在 Sl
的构造函数中初始化。
这种初始化暗示了复制构造函数的使用。
然而,正如编译器的消息所述,如果我们提供自己版本的复制赋值运算符,这当然意味着在这样的复制操作中存在一些微妙的东西,那么复制构造函数可能应该是也明确提供。
如果没有提供 copy-assign 操作符,那么默认的被认为是合适的,并且默认的 copy-constructor 可能也合适。
请注意,您应该始终考虑 rule of five or zero。
,您正在为 lcl(cl)
使用复制构造函数,尽管您没有定义它,但您定义了一个用户定义的赋值运算符。您可以通过添加用户定义的构造函数来修复它:
class Cl {
private:
float f;
public:
constexpr float GetF() const { return f; }
constexpr Cl(const Cl& other) : f(other.f) { }
Cl& operator=(const Cl& other) {
f = other.GetF();
return *this;
}
Cl& operator=(const float& other) {
this->f = other;
return *this;
}
explicit constexpr Cl(const float& val) : f(val) {}
};
struct Sl {
float x,y;
Cl lcl;
constexpr Sl(const float &init_x,const float &init_y,const Cl &cl) : x(init_x),y(init_y),lcl(cl) {}
};
typedef struct Sl sdata;
int main()
{
const float fx = 30.30;
Cl c1(fx);
sdata s1(0,0.0,c1);
return 0;
}
Nitpick:“错误的含义是什么?”这不是错误而是警告。该项目可以编译并按预期运行,当然,问题应该得到解决。