第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

為什么我不能將唯一的PTR推回向量?

為什么我不能將唯一的PTR推回向量?

C++
一只萌萌小番薯 2019-07-05 13:31:25
為什么我不能將唯一的PTR推回向量?這個(gè)程序有什么問題?#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;}錯(cuò)誤:In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/mingw32/bits/c++allocator.h:34:0,                 from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/allocator.h:48,                 from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/memory:64,                 from main.cpp:6:c:\mingw\bin\../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:\mingw\bin\../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 herec:\mingw\bin\../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:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/ext/new_allocator.h:105:9: error: used hereIn file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/vector:69:0,                 from main.cpp:7:
查看完整描述

2 回答

?
慕標(biāo)5832272

TA貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊

你需要移動(dòng)unique_ptr:

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

unique_ptr保證unique_ptr容器擁有持有指針的所有權(quán)。這意味著您不能復(fù)制unique_ptr(因?yàn)槟菚r(shí)有兩個(gè)unique_ptr我們有所有權(quán)),所以你只能移動(dòng)它。

但是,請注意,您當(dāng)前使用的unique_ptr是不正確的。不能使用它來管理指向局部變量的指針。局部變量的生存期是自動(dòng)管理的:局部變量在塊結(jié)束時(shí)被銷毀(例如,當(dāng)函數(shù)返回時(shí),在本例中)。您需要?jiǎng)討B(tài)分配對象:

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


查看完整回答
反對 回復(fù) 2019-07-05
?
牛魔王的故事

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超3個(gè)贊

STD:UNIQUE_PTR沒有復(fù)制構(gòu)造函數(shù)。創(chuàng)建一個(gè)實(shí)例,然后詢問STD:向量若要在初始化期間復(fù)制該實(shí)例,請執(zhí)行以下操作。

error: deleted function 'std::unique_ptr<_Tp, _Tp_Deleter>::uniqu
e_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = int, _Tp_D
eleter = std::default_delete<int>, std::unique_ptr<_Tp, _Tp_Deleter> =
 std::unique_ptr<int>]'

類滿足MoveConstrucable和MoveAssignable的要求,但不滿足CopyConstrucable或CopyAssignable的要求。

以下內(nèi)容與新的座落打電話。

std::vector< std::unique_ptr< int > > vec;vec.emplace_back( new int( 1984 ) );

看見在標(biāo)準(zhǔn)庫容器中使用UNIQUE_PTR進(jìn)一步閱讀。


查看完整回答
反對 回復(fù) 2019-07-05
  • 2 回答
  • 0 關(guān)注
  • 396 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)