Я хочу создать std::vector<std::vector<LibraryClass>>
, где LibraryClass
мне недоступен для модификации и не имеет конструктора копирования.
Для std::vector<LibraryClass>
компилируется следующий код:
#include <vector>
class LibraryClass {
public:
LibraryClass() {};
LibraryClass(const LibraryClass&) = delete;
};
class MainClass {
public:
MainClass(int);
std::vector<LibraryClass> collection;
};
MainClass::MainClass(int nx)
: collection( nx ) {
}
int main() {
MainClass main_class(3);
}
Но с std::vector<std::vector<LibraryClass>>
этого не происходит (использование удаленной функции - конструктора копирования):
#include <vector>
class LibraryClass {
public:
LibraryClass() {};
LibraryClass(const LibraryClass&) = delete;
};
class MainClass {
public:
MainClass(int, int);
std::vector<std::vector<LibraryClass>> collection;
};
MainClass::MainClass(int nx, int ny)
: collection( nx, std::vector<LibraryClass>(ny) ) {
}
int main() {
MainClass main_class(3, 4);
}
Что я могу сделать для этого?
Заранее спасибо.
РЕДАКТИРОВАТЬ: После компиляции с g++ -o test_vec_of_ves test_vec_of_vecs.cpp
точная ошибка:
In file included from /usr/include/c++/7/vector:62:0,
from test_vec_of_vecs.cpp:1:
/usr/include/c++/7/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = LibraryClass; _Args = {const LibraryClass&}]’:
/usr/include/c++/7/bits/stl_uninitialized.h:83:18: required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const LibraryClass*, std::vector<LibraryClass> >; _ForwardIterator = LibraryClass*; bool _TrivialValueTypes = false]’
/usr/include/c++/7/bits/stl_uninitialized.h:134:15: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const LibraryClass*, std::vector<LibraryClass> >; _ForwardIterator = LibraryClass*]’
/usr/include/c++/7/bits/stl_uninitialized.h:289:37: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const LibraryClass*, std::vector<LibraryClass> >; _ForwardIterator = LibraryClass*; _Tp = LibraryClass]’
/usr/include/c++/7/bits/stl_vector.h:331:31: required from ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = LibraryClass; _Alloc = std::allocator<LibraryClass>]’
/usr/include/c++/7/bits/stl_construct.h:75:7: required from ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::vector<LibraryClass>; _Args = {const std::vector<LibraryClass, std::allocator<LibraryClass> >&}]’
/usr/include/c++/7/bits/stl_uninitialized.h:210:18: required from ‘static _ForwardIterator std::__uninitialized_fill_n<_TrivialValueType>::__uninit_fill_n(_ForwardIterator, _Size, const _Tp&) [with _ForwardIterator = std::vector<LibraryClass>*; _Size = long unsigned int; _Tp = std::vector<LibraryClass>; bool _TrivialValueType = false]’
/usr/include/c++/7/bits/stl_uninitialized.h:255:17: required from ‘_ForwardIterator std::uninitialized_fill_n(_ForwardIterator, _Size, const _Tp&) [with _ForwardIterator = std::vector<LibraryClass>*; _Size = long unsigned int; _Tp = std::vector<LibraryClass>]’
/usr/include/c++/7/bits/stl_uninitialized.h:366:39: required from ‘_ForwardIterator std::__uninitialized_fill_n_a(_ForwardIterator, _Size, const _Tp&, std::allocator<_Tp2>&) [with _ForwardIterator = std::vector<LibraryClass>*; _Size = long unsigned int; _Tp = std::vector<LibraryClass>; _Tp2 = std::vector<LibraryClass>]’
/usr/include/c++/7/bits/stl_vector.h:1342:33: required from ‘void std::vector<_Tp, _Alloc>::_M_fill_initialize(std::vector<_Tp, _Alloc>::size_type, const value_type&) [with _Tp = std::vector<LibraryClass>; _Alloc = std::allocator<std::vector<LibraryClass> >; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = std::vector<LibraryClass>]’
/usr/include/c++/7/bits/stl_vector.h:298:27: required from ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = std::vector<LibraryClass>; _Alloc = std::allocator<std::vector<LibraryClass> >; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = std::vector<LibraryClass>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::vector<LibraryClass> >]’
test_vec_of_vecs.cpp:17:49: required from here
/usr/include/c++/7/bits/stl_construct.h:75:7: error: use of deleted function ‘LibraryClass::LibraryClass(const LibraryClass&)’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test_vec_of_vecs.cpp:6:3: note: declared here
LibraryClass(const LibraryClass&) = delete;
^~~~~~~~~~~~