久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么我不能將 unique_ptr 推回到向量中?

Why can I not push_back a unique_ptr into a vector?(為什么我不能將 unique_ptr 推回到向量中?)
本文介紹了為什么我不能將 unique_ptr 推回到向量中?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

這個程序有什么問題?

#include <memory>
#include <vector>

int main()
{
    std::vector<std::unique_ptr<int>> vec;

    int x(1);
    std::unique_ptr<int> ptr2x(&x);
    vec.push_back(ptr2x); //This tiny command has a vicious error.

    return 0;
}

錯誤:

In file included from c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/mingw32/bits/c++allocator.h:34:0,
                 from c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/bits/allocator.h:48,
                 from c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/memory:64,
                 from main.cpp:6:
c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/bits/unique_ptr.h: In member function 'void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = std::unique_ptr<int>, _Tp* = std::unique_ptr<int>*]':
c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_vector.h:745:6:   instantiated from 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::unique_ptr<int>, _Alloc = std::allocator<std::unique_ptr<int> >, value_type = std::unique_ptr<int>]'
main.cpp:16:21:   instantiated from here
c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/bits/unique_ptr.h:207:7: error: deleted function 'std::unique_ptr<_Tp, _Tp_Deleter>::unique_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = int, _Tp_Deleter = std::default_delete<int>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<int>]'
c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/ext/new_allocator.h:105:9: error: used here
In file included from c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/vector:69:0,
                 from main.cpp:7:
c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/bits/unique_ptr.h: In member function 'void std::vector<_Tp, _Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>::iterator, _Args&& ...) [with _Args = {const std::unique_ptr<int>&}, _Tp = std::unique_ptr<int>, _Alloc = std::allocator<std::unique_ptr<int> >, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<std::unique_ptr<int>*, std::vector<std::unique_ptr<int> > >, typename std::vector<_Tp, _Alloc>::_Base::_Tp_alloc_type::pointer = std::unique_ptr<int>*]':
c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_vector.h:749:4:   instantiated from 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::unique_ptr<int>, _Alloc = std::allocator<std::unique_ptr<int> >, value_type = std::unique_ptr<int>]'
main.cpp:16:21:   instantiated from here
c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/bits/unique_ptr.h:207:7: error: deleted function 'std::unique_ptr<_Tp, _Tp_Deleter>::unique_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = int, _Tp_Deleter = std::default_delete<int>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<int>]'
c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/bits/vector.tcc:314:4: error: used here

推薦答案

你需要移動 unique_ptr:

You need to move the unique_ptr:

vec.push_back(std::move(ptr2x));

unique_ptr 保證單個 unique_ptr 容器擁有持有的指針的所有權(quán).這意味著您不能復(fù)制 unique_ptr(因為兩個 unique_ptr 將擁有所有權(quán)),因此您只能移動它.

unique_ptr guarantees that a single unique_ptr container has ownership of the held pointer. This means that you can't make copies of a unique_ptr (because then two unique_ptrs would have ownership), so you can only move it.

但是請注意,您當前使用的 unique_ptr 是不正確的.您不能使用它來管理指向局部變量的指針.局部變量的生命周期是自動管理的:局部變量在塊結(jié)束時被銷毀(例如,在這種情況下,當函數(shù)返回時).您需要動態(tài)分配對象:

Note, however, that your current use of unique_ptr is incorrect. You cannot use it to manage a pointer to a local variable. The lifetime of a local variable is managed automatically: local variables are destroyed when the block ends (e.g., when the function returns, in this case). You need to dynamically allocate the object:

std::unique_ptr<int> ptr(new int(1));

在 C++14 中,我們有一個更好的方法:

In C++14 we have an even better way to do so:

make_unique<int>(5);

這篇關(guān)于為什么我不能將 unique_ptr 推回到向量中?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉(zhuǎn)換為 HSV 并將 HSV 轉(zhuǎn)換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類型變量轉(zhuǎn)換為字符串?)
When to use inline function and when not to use it?(什么時候使用內(nèi)聯(lián)函數(shù),什么時候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意義;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相關(guān)嗎?)
主站蜘蛛池模板: 亚洲欧美日韩国产综合 | 日韩精品不卡 | 成人区精品一区二区婷婷 | 五月天婷婷狠狠 | 日本一二三区在线观看 | 免费视频二区 | 二区中文字幕 | 精品国产乱码 | 一区二区影院 | 国产麻豆一区二区三区 | 在线免费毛片 | 成人av鲁丝片一区二区小说 | 国产一二三区在线 | 国产欧美精品一区二区色综合 | 国产精品激情 | 日韩精品一二三区 | 国产在线视频一区 | 成年人黄色一级片 | 色男人的天堂 | 四虎影院免费在线播放 | 日本 欧美 国产 | 国产偷久久一级精品60部 | 成人在线免费 | 91一区二区三区 | 亚洲欧美日韩中文字幕一区二区三区 | 国产免费高清 | 三极网站| 久久久久国产精品一区三寸 | 亚洲一区二区视频 | 国产精品久久久久久久久久久久午夜片 | 蜜桃视频在线观看免费视频网站www | 中文字幕第十一页 | 成人国产毛片 | 欧美性jizz18性欧美 | 91亚洲精品国偷拍自产在线观看 | 日本一区二区不卡视频 | 久久久久久国产精品免费免费狐狸 | 91麻豆精品国产91久久久久久 | 国产日韩电影 | 成人精品在线观看 | 亚洲九九 |