Solidity

ParserError:預期的“:”但得到了“應付”

  • June 1, 2021
function buyStar(uint256 _tokenId) public payable {
       require(starsForSale[_tokenId] > 0, "The star must be available for sale!"); // the star to buy must be in starsForSale mapping
       uint256 starPrice = starsForSale[_tokenId]; // get the price of the star for sale
       address starOwner = ownerOf(_tokenId); // get the owner of the star
       require(msg.value >= starPrice, "You need to have sufficient funds!"); // check the bidding price is greater than or equal to the stars for sale price
       _transfer(starOwner, msg.sender, _tokenId);
       address payable starOwnerAddressPayable = payable(starOwner);
       starOwnerAddressPayable.transfer(starPrice);
       if(msg.value > starPrice) {
           msg.sender.transfer(msg.value - starPrice);
       }
   }

出現的錯誤是 ParserError: Expected ';' but got 'payable' address payable starOwnerAddressPayable = starOwner;

編譯失敗。往上看。

克服這個問題的最新方法是什麼。

是的,錯誤是一致的

address payable starOwnerAddressPayable = payable(starOwner);

已通過使用 _make_payable 功能糾正。解決了。

最終程式碼審查-

function _make_payable(address x) internal pure returns (address payable) {
       return payable(address(uint160(x)));
   }

   function buyStar(uint256 _tokenId) public payable {
       require(starsForSale[_tokenId] > 0, "The star must be available for sale!"); // the star to buy must be in starsForSale mapping
       uint256 starPrice = starsForSale[_tokenId]; // get the price of the star for sale
       address starOwner = ownerOf(_tokenId); // get the owner of the star
       require(msg.value >= starPrice, "You need to have sufficient funds!"); // check the bidding price is greater than or equal to the stars for sale price
       _transfer(starOwner, msg.sender, _tokenId);
       address payable starOwnerAddressPayable = _make_payable(starOwner);
       address payable newOwnerAddressPayable = _make_payable(msg.sender);
       starOwnerAddressPayable.transfer(starPrice);
       if(msg.value > starPrice) {
           newOwnerAddressPayable.transfer(msg.value - starPrice);
       }
   }

我不知道你的實際程式碼是什麼,所以我製作了函式供參考,它對我來說很好用。

pragma solidity ^0.8.0;


contract test {
   uint256[] starsForSale;
   
   function ownerOf(uint256 some) public returns(address){
       return msg.sender;
   }
   
   function _transfer(address from, address _to, uint256 amount) public {
       
   }
   

   function buyStar(uint256 _tokenId) public payable {
       require(starsForSale[_tokenId] > 0, "The star must be available for sale!"); // the star to buy must be in starsForSale mapping
       uint256 starPrice = starsForSale[_tokenId]; // get the price of the star for sale
       address starOwner = ownerOf(_tokenId); // get the owner of the star
       require(msg.value >= starPrice, "You need to have sufficient funds!"); // check the bidding price is greater than or equal to the stars for sale price
       _transfer(starOwner, msg.sender, _tokenId);
       address payable starOwnerAddressPayable = payable(starOwner);
       starOwnerAddressPayable.transfer(starPrice);
       if(msg.value > starPrice) {
           payable(msg.sender).transfer(msg.value - starPrice);
       }
   }
}

我認為你在address payable starOwnerAddressPayable = starOwner;.

但是在您編寫的程式碼中address payable starOwnerAddressPayable = payable(starOwner);,我認為您忘記了保存?

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