创建一个 std::vector of std::vectors of class 没有复制构造函数
Create a std::vector of std::vectors of class without copy constructor
我想创建一个 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;
^~~~~~~~~~~~
按照 underscore_d 的建议,这里是一个似乎可以工作的版本(包括一些表明某事已完成的功能)。
#include <vector>
#include <iostream>
#include <memory>
class LibraryClass {
public:
LibraryClass() {};
LibraryClass(const LibraryClass&) = delete;
void init(int aa, int bb) { a = aa; b = bb; };
void report() { std::cout << "I am " << a << " / " << b << std::endl; };
private:
int a, b;
};
class MainClass {
public:
MainClass(int, int);
void init();
void report();
std::vector<std::vector<std::unique_ptr<LibraryClass>>> collection;
};
MainClass::MainClass(int nx, int ny) {
collection.resize(nx);
for (int i=0; i<nx; ++i) {
collection[i].resize(ny);
for (int j=0; j<ny; ++j) {
collection[i][j] = std::make_unique<LibraryClass>();
}
}
}
void MainClass::init() {
for (int i=0; i<collection.size(); ++i) {
for (int j=0; j<collection[i].size(); ++j) {
collection[i][j]->init(i,j);
}
}
}
void MainClass::report() {
for (int i=0; i<collection.size(); ++i) {
for (int j=0; j< collection[i].size(); ++j) {
collection[i][j]->report();
}
}
}
int main() {
int nx = 3;
int ny = 4;
MainClass main_class(nx, ny);
main_class.init();
main_class.report();
}
我想创建一个 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;
^~~~~~~~~~~~
按照 underscore_d 的建议,这里是一个似乎可以工作的版本(包括一些表明某事已完成的功能)。
#include <vector>
#include <iostream>
#include <memory>
class LibraryClass {
public:
LibraryClass() {};
LibraryClass(const LibraryClass&) = delete;
void init(int aa, int bb) { a = aa; b = bb; };
void report() { std::cout << "I am " << a << " / " << b << std::endl; };
private:
int a, b;
};
class MainClass {
public:
MainClass(int, int);
void init();
void report();
std::vector<std::vector<std::unique_ptr<LibraryClass>>> collection;
};
MainClass::MainClass(int nx, int ny) {
collection.resize(nx);
for (int i=0; i<nx; ++i) {
collection[i].resize(ny);
for (int j=0; j<ny; ++j) {
collection[i][j] = std::make_unique<LibraryClass>();
}
}
}
void MainClass::init() {
for (int i=0; i<collection.size(); ++i) {
for (int j=0; j<collection[i].size(); ++j) {
collection[i][j]->init(i,j);
}
}
}
void MainClass::report() {
for (int i=0; i<collection.size(); ++i) {
for (int j=0; j< collection[i].size(); ++j) {
collection[i][j]->report();
}
}
}
int main() {
int nx = 3;
int ny = 4;
MainClass main_class(nx, ny);
main_class.init();
main_class.report();
}