Solidity

錯誤類型地址不能隱式轉換為預期的應付類型地址

  • June 23, 2021

我收到以下錯誤

類型地址不能隱式轉換為在撤消函式上應付的預期類型地址。

pragma solidity ^0.8.1;

contract sendMoneyExamples{
   uint public getRecivedMoney;
   function reciveMoney()public payable{
       getRecivedMoney=getRecivedMoney+msg.value;
   }
   function getBalance()view public returns(uint){
       return address(this).balance;
   }
   function withdraw()public {
       address payable to = msg.sender;
       to.transfer(this.getBalance());
   }
   function withdrawato(address payable _to)public{
       _to.transfer(getBalance());
   }
}

’to’ 變數的類型是應付地址,而 msg.sender 的類型是地址。錯誤狀態下,您不能將地址隱式轉換為應付地址。從 to 變數中刪除 payable 關鍵字就可以了。

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