Search code examples
c++shared-ptr

no match for ‘operator=’ when std::shared_ptr


I see a code using exactly this, but that code works and mine not, any idea why?

PD: im trying to implement this commit. See that the code is exactly the same

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;

Solution

  • Read the error message carefully: you are trying to assign an object of type const CTransaction to a shared pointer of type std::shared_ptr<const CTransaction>. But you can't do that using operator=, because its argument should be a shared_ptr or unique_ptr, as described at cppreference.

    Depending on your actual code, I think, you can create a new shared_ptr for the const CTransaction object and then assign to it.