gpt4 book ai didi

c++ - 循环依赖和 std::vector::insert

转载 作者:行者123 更新时间:2023-11-28 05:10:46 27 4
gpt4 key购买 nike

我有这个文件:

啊啊

struct B;

struct A
{
A(... B &b) :
b(b) {}

B &b;
};

B.h:

#include "A.h"
struct B{
...
std::vector<A> as;
}

还有这段代码:

std::vector<A> manya;
std::vector<B> bs;
//fill bs
for(size_t i=0; i<bs.size(); i++)
manya.insert(manya.end(), bs[i].as.begin(), bs[i].as.end());

但是,我收到此错误(其中 A=FindAffineShapeArgsB=Wrapper):

/usr/include/c++/5/bits/stl_algobase.h(564): error: function "FindAffineShapeArgs::operator=(const FindAffineShapeArgs &)" (declared implicitly) cannot be referenced -- it is a deleted function
*--__result = std::move(*--__last);
^
detected during:
instantiation of "_BI2 std::__copy_move_backward<true, false, std::random_access_iterator_tag>::__copy_move_b(_BI1, _BI1, _BI2) [with _BI1=FindAffineShapeArgs *, _BI2=FindAffineShapeArgs *]" at line 606
instantiation of "_BI2 std::__copy_move_backward_a<_IsMove,_BI1,_BI2>(_BI1, _BI1, _BI2) [with _IsMove=true, _BI1=FindAffineShapeArgs *, _BI2=FindAffineShapeArgs *]" at line 615
instantiation of "_BI2 std::__copy_move_backward_a2<_IsMove,_BI1,_BI2>(_BI1, _BI1, _BI2) [with _IsMove=true, _BI1=FindAffineShapeArgs *, _BI2=FindAffineShapeArgs *]" at line 686
instantiation of "_BI2 std::move_backward(_BI1, _BI1, _BI2) [with _BI1=FindAffineShapeArgs *, _BI2=FindAffineShapeArgs *]" at line 636 of "/usr/include/c++/5/bits/vector.tcc"
instantiation of "void std::vector<_Tp, _Alloc>::_M_range_insert(std::vector<_Tp, _Alloc>::iterator, _ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _Tp=FindAffineShapeArgs, _Alloc=std::allocator<FindAffineShapeArgs>, _ForwardIterator=__gnu_cxx::__normal_iterator<FindAffineShapeArgs *, std::vector<FindAffineShapeArgs, std::allocator<FindAffineShapeArgs>>>]" at line 1377 of "/usr/include/c++/5/bits/stl_vector.h"
instantiation of "void std::vector<_Tp, _Alloc>::_M_insert_dispatch(std::vector<_Tp, _Alloc>::iterator, _InputIterator, _InputIterator, std::__false_type) [with _Tp=FindAffineShapeArgs, _Alloc=std::allocator<FindAffineShapeArgs>, _InputIterator=__gnu_cxx::__normal_iterator<FindAffineShapeArgs *, std::vector<FindAffineShapeArgs, std::allocator<FindAffineShapeArgs>>>]" at line 1100 of "/usr/include/c++/5/bits/stl_vector.h"
instantiation of "std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::const_iterator, _InputIterator, _InputIterator) [with _Tp=FindAffineShapeArgs, _Alloc=std::allocator<FindAffineShapeArgs>, _InputIterator=__gnu_cxx::__normal_iterator<FindAffineShapeArgs *, std::vector<FindAffineShapeArgs, std::allocator<FindAffineShapeArgs>>>, <unnamed>=void]" at line 382 of
"/home/luca/Dropbox/HKUST/CloudCache/cloudcache/CloudCache/Descriptors/hesaff/pyramid.cpp"

/usr/include/c++/5/bits/stl_algobase.h(340): error: function "FindAffineShapeArgs::operator=(const FindAffineShapeArgs &)" (declared implicitly) cannot be referenced -- it is a deleted function
*__result = *__first;
^
detected during:
instantiation of "_OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II=FindAffineShapeArgs *, _OI=FindAffineShapeArgs *]" at line 402
instantiation of "_OI std::__copy_move_a<_IsMove,_II,_OI>(_II, _II, _OI) [with _IsMove=false, _II=FindAffineShapeArgs *, _OI=FindAffineShapeArgs *]" at line 440
instantiation of "_OI std::__copy_move_a2<_IsMove,_II,_OI>(_II, _II, _OI) [with _IsMove=false, _II=__gnu_cxx::__normal_iterator<FindAffineShapeArgs *, std::vector<FindAffineShapeArgs, std::allocator<FindAffineShapeArgs>>>, _OI=__gnu_cxx::__normal_iterator<FindAffineShapeArgs *, std::vector<FindAffineShapeArgs, std::allocator<FindAffineShapeArgs>>>]" at line 472
instantiation of "_OI std::copy(_II, _II, _OI) [with _II=__gnu_cxx::__normal_iterator<FindAffineShapeArgs *, std::vector<FindAffineShapeArgs, std::allocator<FindAffineShapeArgs>>>, _OI=__gnu_cxx::__normal_iterator<FindAffineShapeArgs *, std::vector<FindAffineShapeArgs, std::allocator<FindAffineShapeArgs>>>]" at line 637 of "/usr/include/c++/5/bits/vector.tcc"
instantiation of "void std::vector<_Tp, _Alloc>::_M_range_insert(std::vector<_Tp, _Alloc>::iterator, _ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _Tp=FindAffineShapeArgs, _Alloc=std::allocator<FindAffineShapeArgs>, _ForwardIterator=__gnu_cxx::__normal_iterator<FindAffineShapeArgs *, std::vector<FindAffineShapeArgs, std::allocator<FindAffineShapeArgs>>>]" at line 1377 of "/usr/include/c++/5/bits/stl_vector.h"
instantiation of "void std::vector<_Tp, _Alloc>::_M_insert_dispatch(std::vector<_Tp, _Alloc>::iterator, _InputIterator, _InputIterator, std::__false_type) [with _Tp=FindAffineShapeArgs, _Alloc=std::allocator<FindAffineShapeArgs>, _InputIterator=__gnu_cxx::__normal_iterator<FindAffineShapeArgs *, std::vector<FindAffineShapeArgs, std::allocator<FindAffineShapeArgs>>>]" at line 1100 of "/usr/include/c++/5/bits/stl_vector.h"
instantiation of "std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::const_iterator, _InputIterator, _InputIterator) [with _Tp=FindAffineShapeArgs, _Alloc=std::allocator<FindAffineShapeArgs>, _InputIterator=__gnu_cxx::__normal_iterator<FindAffineShapeArgs *, std::vector<FindAffineShapeArgs, std::allocator<FindAffineShapeArgs>>>, <unnamed>=void]" at line 382 of
"/home/luca/Dropbox/HKUST/CloudCache/cloudcache/CloudCache/Descriptors/hesaff/pyramid.cpp"

compilation aborted for /home/luca/Dropbox/HKUST/CloudCache/cloudcache/CloudCache/Descriptors/hesaff/pyramid.cpp (code 2)

为什么会这样?

这可能与 this 有关但对于不同的情况

最佳答案

问题是你的结构 A有引用成员(member)B& b .由于无法重新分配引用,因此编译器无法为您隐式生成复制或移动赋值运算符。

但是,对于 std::vector<T>::insert 的这个特殊重载, 标准 ( [sequence.reqmts]/4 ) 状态

Requires: T shall be EmplaceConstructible into X from *i. For vector and deque, T shall also be MoveInsertable into X, MoveConstructible, MoveAssignable, and swappable (17.6.3.2). [...]

这就是编译器提示隐式删除赋值运算符的原因。

如果你真的想让它成为引用成员,你可以使用std::reference_wrapper<B>而不是 B&在结构内部 A .

关于c++ - 循环依赖和 std::vector::insert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43550191/

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