簡體   English   中英

當 std::shared_ptr 不匹配 'operator='

[英]no match for ‘operator=’ when std::shared_ptr

我看到一個代碼正是使用這個,但是那個代碼有效而我的無效,知道為什么嗎?

PD:我試圖實現這個提交 看到代碼完全一樣

for(const auto& tx : block.vtx)
    if (txHash == tx->GetHash()) {
        txNew = tx;
        foundAtOut = *pindex;
        return true;
    }
main.cpp:2471:25: error: no match for ‘operator=’ (operand types are ‘CTransactionRef’ {aka ‘std::shared_ptr<const CTransaction>’} and ‘const CTransaction’)
             txNew = tx;

仔細閱讀錯誤消息:您正在嘗試將const CTransaction類型的 object 分配給std::shared_ptr<const CTransaction>類型的共享指針。 但是您不能使用operator=來做到這一點,因為它的參數應該是shared_ptrunique_ptr ,如cppreference所述。

我認為,根據您的實際代碼,您可以為const CTransaction object 創建一個新的shared_ptr然后分配給它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM