gpt4 book ai didi

c++ - 编译器报告 'deleted' operator = ,但它在那里

转载 作者:行者123 更新时间:2023-11-30 00:43:59 25 4
gpt4 key购买 nike

我遇到了一个棘手的问题,编译器声明了一个 operator=被删除了,但它在那里。经过几个小时的尝试,我制作了一个重现该问题的最小解决方案。我正在使用 MSVC Community Edition 2017 15.7.5(截至今天,2018-07-20 是最新的),并将其设置为“C++17”

代码并不简单;这个概念是模板类 TT 用于强制静态成员函数的存在 foo在一组类(class)中Fn .这与工厂模式非常相似,只是此解决方案不创建类实例,而是报告有关该类的静态详细信息。

错误在最后一行的分配中报告,并显示为(底部的完整错误列表):

"error C2280: 'C &C::operator =(const C &)': attempting to reference a deleted function"

但是第 5 行定义了这个运算符,就和那些装饰器一样?

失败的分配尝试分配返回的 const std::vector<C>&到类成员变量。
我以为 const正在产生问题,但删除了所有 const在每个函数中没有任何区别;同一行出现同样的错误。

问题:为什么编译器会报告此问题,可能的解决方法是什么?
我想这一定是我想念的愚蠢的东西,但我找不到它。

#include <vector>

class C
{
public:
C(int ii) : i(ii) {}
C& operator=(const C&) = default; /// HERE is the assignment operator
const int i;
};
typedef std::vector<C> CVec; // shorthand for a vector of C's

template <class T> // this template forces classes F1, F2, ... to have a static member function 'foo'
class TT {
public:
static const CVec& foo(void) { return T::foo(); }
};

class F1 // one of many Fn classes
{
public:
static const CVec& foo(void) { static CVec cv{ C{ 1 }, C{ 2 } }; return cv; } // static member as forced by template
//...
};
class F2 // another one of many Fn classes
{
public:
static const CVec& foo(void) { static CVec cv{ C{ 3 } }; return cv; } // static member as forced by template
//...
};

class D // controller class
{
public:
CVec cv;
const CVec& bar(int z) // function to select one of the subclasses
{
switch (z)
{
case 1: return TT<F1>::foo();
case 2: return TT<F2>::foo();
//...
}
}

void foobar(void) //selector (from user input)
{
int z = 2; // user input
cv = bar(z); // THIS assignment produces the error
}
};

完整错误文本:

------ Build started: Project: BG, Configuration: Debug Win32 ------
bgcore.cpp
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(2443): error C2280: 'C &C::operator =(const C &)': attempting to reference a deleted function
d:\projects\bg\core\bgcore.h(8): note: see declaration of 'C::operator ='
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(2462): note: see reference to function template instantiation '_OutIt std::_Copy_unchecked1<_InIt,_OutIt>(_InIt,_InIt,_OutIt,std::_General_ptr_iterator_tag)' being compiled
with
[
_OutIt=C *,
_InIt=C *
]
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\vector(1430): note: see reference to function template instantiation '_OutIt *std::_Copy_unchecked<_Iter,C*>(_InIt,_InIt,_OutIt)' being compiled
with
[
_OutIt=C *,
_Iter=C *,
_InIt=C *
]
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\vector(1448): note: see reference to function template instantiation 'void std::vector<C,std::allocator<_Ty>>::_Assign_range<_Iter>(_Iter,_Iter,std::forward_iterator_tag)' being compiled
with
[
_Ty=C,
_Iter=C *
]
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\vector(1448): note: see reference to function template instantiation 'void std::vector<C,std::allocator<_Ty>>::_Assign_range<_Iter>(_Iter,_Iter,std::forward_iterator_tag)' being compiled
with
[
_Ty=C,
_Iter=C *
]
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\vector(1471): note: see reference to function template instantiation 'void std::vector<C,std::allocator<_Ty>>::assign<C*,void>(_Iter,_Iter)' being compiled
with
[
_Ty=C,
_Iter=C *
]
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\vector(1471): note: see reference to function template instantiation 'void std::vector<C,std::allocator<_Ty>>::assign<C*,void>(_Iter,_Iter)' being compiled
with
[
_Ty=C,
_Iter=C *
]
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\vector(1457): note: while compiling class template member function 'std::vector<C,std::allocator<_Ty>> &std::vector<_Ty,std::allocator<_Ty>>::operator =(const std::vector<_Ty,std::allocator<_Ty>> &)'
with
[
_Ty=C
]
d:\projects\bg\core\bgcore.h(49): note: see reference to function template instantiation 'std::vector<C,std::allocator<_Ty>> &std::vector<_Ty,std::allocator<_Ty>>::operator =(const std::vector<_Ty,std::allocator<_Ty>> &)' being compiled
with
[
_Ty=C
]
d:\projects\bg\core\bgcore.h(22): note: see reference to class template instantiation 'std::vector<C,std::allocator<_Ty>>' being compiled
with
[
_Ty=C
]
Done building project "BG.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最佳答案

这是您的问题的更短版本:

class C
{
public:
C(int ii) : i(ii) {}
C& operator=(const C&) = default;
const int i;
};

C a(1);
a = a; // error: use of deleted function

虽然您默认 函数,但这并不意味着它一定有效。这只是意味着您明确默认它。默认的复制赋值运算符将一个接一个地复制分配所有子对象和成员。但是你的一个成员是 const int,你不能复制分配它!它是 常量!

具体规则在[class.copy.assign]/7 :

A defaulted copy/move assignment operator for class X is defined as deleted if X has: [...] a non-static data member of const non-class type (or array thereof), or [...]

让成员只是 int i,就可以了。

关于c++ - 编译器报告 'deleted' operator = ,但它在那里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51435102/

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