Solidity

如何修復“類型地址不能隱式轉換為預期的應付類型地址”?

  • October 3, 2022

這是功能:

  function withdrawMoney() public {
       address payable to = msg.sender;
       to.transfer(this.getBalance());
   }

和錯誤:

TypeError: Type address is not implicitly convertible to expected type address payable.
 --> pay.sol:16:9:
  |
16 |         address payable to = msg.sender;
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

有人建議刪除payable關鍵字,但是我不能在下一行使用transfer關鍵字。

在 Solidity 版本之前0.8.0,可以將 type 隱式轉換為addresstype address payable

但是從 Solidity 版本0.8.0開始,我們需要顯式地將address類型轉換為address payablelike:

address payable to = payable(msg.sender);

顯式轉換:

address payable to = payable(msg.sender);

引用自:https://ethereum.stackexchange.com/questions/136782