gpt4 book ai didi

c++ - 创建没有复制构造函数的std::vectors的std::vector

转载 作者:行者123 更新时间:2023-12-02 10:01:26 26 4
gpt4 key购买 nike

我想创建一个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();
}

关于c++ - 创建没有复制构造函数的std::vectors的std::vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62406233/

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