错误C2582: 'operator ='函数在'B'中不可用

时间:2022-07-16 09:44:22

Following code gives a compilation error (at least when using MS VS 2008) for line "e=f" in main():

下面的代码给出了main()中的“e=f”的编译错误(至少在使用MS VS 2008时是这样):

error C2582: 'operator =' function is unavailable in 'B'

错误C2582: 'operator ='函数在'B'中不可用

class A {
public:
    A() { }
    static const double x;
};
const double A::x = 0.0;

class B {
public:
    B() : x(0.0) { }
    const double x;
};

int main( int argc, char *argv[] )
{
    A c,d;
    B e,f;

    c = d;
    e = f;

    return 0;
}

The default assignment operator should be generated for both classes, A and B !?

应该为A类和B类生成默认的赋值操作符!

in 12.8.10: "If the class definition does not explicitly declare a copy assignment operator, one is declared implicitly."

在12.8.10:“如果类定义没有显式地声明一个复制赋值操作符,那么将隐式地声明一个。”

3 个解决方案

#1


14  

The implicitly generated operator would recursively assign each non-static member. However, x is const, so it can't be assigned to. This prevents the implicit operator from being generated (specifically, it causes it to be defined as deleted).

隐式生成的运算符将递归地分配每个非静态成员。但是,x是const,所以不能赋值给它。这阻止了隐式运算符的生成(具体地说,它使隐式运算符被定义为删除)。

This is specified in C++11 12.8/23:

这在c++ 11.12.8 /23中规定:

A defaulted copy/move assignment operator for class X is defined as deleted if X has:

类X的默认复制/移动赋值操作符定义为删除,如果X有:

  • ...
  • a non-static data member of const non-class type (or array thereof), or
  • const非类类型(或其数组)的非静态数据成员,或
  • ...

(Although I just noticed that your compiler predates C++11; the rules are similar, but specified in different language, in older dialects with no concept of "deleted" functions).

(虽然我刚刚注意到您的编译器在c++ 11之前;规则是相似的,但是用不同的语言指定,使用没有“删除”函数概念的旧方言)。

If you want an assignment operator for a class whose members (or base classes) can't be reassigned, you'll have to define it yourself.

如果您想要为不能重新分配成员(或基类)的类指定赋值操作符,您必须自己定义它。

In class A, the constant member is static, so doesn't form part of an object. Therefore, it doesn't prevent an (empty) object from being assigned to.

在类A中,常量成员是静态的,因此不构成对象的一部分。因此,它不能阻止(空)对象被分配给它。

#2


4  

field 'x' is of const-qualified type of const double, the default assignment operator will be meaningless and is implicitly deleted here

字段'x'是const double的const限定类型,默认赋值操作符没有意义,在这里隐式删除

#3


2  

The obvious difference between class A and B is const member x beeing static vs. non-static. Assignment to a const variable is/should be impossible in any case.

A类和B类之间的明显区别是const成员x是静态的还是非静态的。分配给一个常量变量在任何情况下都是不可能的。

The compiler obviously tries to generate the default assignment operator method for class B and silently decides to not generating one, as member x does not allow assignment.

显然,编译器试图为B类生成默认的赋值操作符方法,并决定不生成一个,因为成员x不允许赋值。

Took me quite a long time to find out this ...

我花了很长时间才发现……

BTW: If you omit the initialization of x in class B , compiler is smart enough to find out this mistake.

顺便说一句:如果你忽略了类B中的x的初始化,编译器会很聪明地发现这个错误。

#1


14  

The implicitly generated operator would recursively assign each non-static member. However, x is const, so it can't be assigned to. This prevents the implicit operator from being generated (specifically, it causes it to be defined as deleted).

隐式生成的运算符将递归地分配每个非静态成员。但是,x是const,所以不能赋值给它。这阻止了隐式运算符的生成(具体地说,它使隐式运算符被定义为删除)。

This is specified in C++11 12.8/23:

这在c++ 11.12.8 /23中规定:

A defaulted copy/move assignment operator for class X is defined as deleted if X has:

类X的默认复制/移动赋值操作符定义为删除,如果X有:

  • ...
  • a non-static data member of const non-class type (or array thereof), or
  • const非类类型(或其数组)的非静态数据成员,或
  • ...

(Although I just noticed that your compiler predates C++11; the rules are similar, but specified in different language, in older dialects with no concept of "deleted" functions).

(虽然我刚刚注意到您的编译器在c++ 11之前;规则是相似的,但是用不同的语言指定,使用没有“删除”函数概念的旧方言)。

If you want an assignment operator for a class whose members (or base classes) can't be reassigned, you'll have to define it yourself.

如果您想要为不能重新分配成员(或基类)的类指定赋值操作符,您必须自己定义它。

In class A, the constant member is static, so doesn't form part of an object. Therefore, it doesn't prevent an (empty) object from being assigned to.

在类A中,常量成员是静态的,因此不构成对象的一部分。因此,它不能阻止(空)对象被分配给它。

#2


4  

field 'x' is of const-qualified type of const double, the default assignment operator will be meaningless and is implicitly deleted here

字段'x'是const double的const限定类型,默认赋值操作符没有意义,在这里隐式删除

#3


2  

The obvious difference between class A and B is const member x beeing static vs. non-static. Assignment to a const variable is/should be impossible in any case.

A类和B类之间的明显区别是const成员x是静态的还是非静态的。分配给一个常量变量在任何情况下都是不可能的。

The compiler obviously tries to generate the default assignment operator method for class B and silently decides to not generating one, as member x does not allow assignment.

显然,编译器试图为B类生成默认的赋值操作符方法,并决定不生成一个,因为成员x不允许赋值。

Took me quite a long time to find out this ...

我花了很长时间才发现……

BTW: If you omit the initialization of x in class B , compiler is smart enough to find out this mistake.

顺便说一句:如果你忽略了类B中的x的初始化,编译器会很聪明地发现这个错误。