[英]'Type non-static const member can't use default assignment operator' - what does this mean?
This class is flagging up the following error: 'non-static const member 'const int Member::membershipNo', can't use default assignment operator'. 此类标记以下错误:'非静态const成员'const int成员:: membershipNo',不能使用默认赋值运算符'。 The strange thing is this code is repeated in another project and works perfectly.
奇怪的是这个代码在另一个项目中重复并完美运行。 Can you help me put it right?
你能帮我把它搞定吗?
Member.h Member.h
class Member : public Person
{
public:
Member();
Member(int membershipNo);
virtual ~Member();
int getMembershipNo() const;
private:
const int membershipNo;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & boost::serialization::base_object<Person>(*this);
ar & membershipNo;
}
};
Member.cpp Member.cpp
Member::Member() : membershipNo(0)
{
Person();
}
Member::Member(int memberNo) : membershipNo(memberNo)
{
Person();
}
Member::~Member()
{
// TODO Auto-generated destructor stub
}
int Member::getMembershipNo() const
{
return membershipNo;
}
Presumably, somewhere in your code you are assigning to a Member
, something like this: 据推测,您在代码中的某个位置指定给
Member
,如下所示:
Member m1, m2;
m1 = m2;
Or you are using it in a context that requires the type to be assignable. 或者您在需要可分配类型的上下文中使用它。
Since you don't provide your own assignment operator overload for Member
, the implicitly defined defaulted assignment operator would usually kick in. However, since you have a const
data member, the compiler won't implicitly define one for you. 由于您没有为
Member
提供自己的赋值运算符重载,因此隐式定义的默认赋值运算符通常会启动。但是,由于您有const
数据成员,编译器不会为您隐式定义一个。 You need to provide it yourself. 你需要自己提供。
This makes sense because, imagine in the code sample I just gave, what should the compiler do to m1
's membershipNo
member? 这是有道理的,因为在我刚给出的代码示例中想象一下,编译器应该对
m1
的membershipNo
成员做什么? Should it assign m2
's membershipNo
to it? 它应该分配
m2
的membershipNo
吗? How can it do that if membershipNo
is const
? 如果
membershipNo
是const
,它怎么能这样做呢? In this case, the compiler just says "Nope, I can't do it." 在这种情况下,编译器只是说“不,我不能这样做”。
A defaulted copy/move assignment operator for class
X
is defined as deleted ifX
has:如果
X
具有以下内容,则将类X
默认复制/移动赋值运算符定义为已删除:
- a non-static data member of
const
non-class type (or array thereof)const
非类类型(或其数组)的非静态数据成员- [...]
[...]
As I said, to be able to do assignment with a Member
, you need to provide an assignment operator of your own: 正如我所说,为了能够与
Member
进行任务,您需要提供自己的任务运营商:
Member& Member::operator=(const Member& other)
{
// Copy things from other to this
}
However, herein lies the problem with having const
members. 然而,这里存在具有
const
成员的问题。 If you supply your own copy assignment operator as described, and you don't copy over membershipNo
, then have you really copied the other object? 如果您按照描述提供自己的复制赋值操作符,并且不复制
membershipNo
,那么您是否真的复制了另一个对象? Logically, it makes sense that an object that has any const
state shouldn't be able to be assigned to. 从逻辑上讲,有意义的是, 不能将具有任何
const
状态的对象分配给。
It is perfectly fine to use a copy constructor however - you only have to make sure you initialise membershipNo
in the member initialization list: 但是,使用复制构造函数是完全正确的 - 您只需要确保在成员初始化列表中初始化
membershipNo
:
Member::Member(const Member& other)
: membershipNo(other.membershipNo)
{
// ...
}
And then you can do: 然后你可以这样做:
Member m1;
Member m2 = m1;
What is happening is that some client code is attempting to use assign one Member
instance to another one. 发生的事情是某些客户端代码试图使用一个
Member
实例分配给另一个Member
实例。 Since you have a constant data member, this cannot work. 由于您有一个常量数据成员,因此无法使用。 The error will only come up if some code attempts to make the assignment, which is why it may seem the same class "works" in another project.
只有当某些代码尝试进行赋值时,才会出现错误,这就是为什么它可能看起来同一个类在另一个项目中“起作用”。
In terms of putting it right, the options are A) not to perform the assignment, or B) make the data member non-const. 在正确的方面,选项是A)不执行赋值,或B)使数据成员非const。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.