Solidity

TypeError:類型 tuple() 不能隱式轉換為預期的 uint256 類型。使用者結構使用者名____d回复___在s和r一種ddr和ssuserAddress.index = userIndex.push(userAddress…

  • October 10, 2021
pragma solidity ^0.6.0;

contract UserCrud{
   struct userStruct{
       uint userAge;
       uint index;
       bytes32 userName;
       bytes32 userEmail;
   }
   mapping(address => userStruct) private userStructs;
   address[] private userIndex;

   function insertUser(
   address userAddress,
   uint userAge,
   bytes32 userName,
   bytes32 userEmail) public returns (uint index) {
       require(!isUser(userAddress));
       userStructs[userAddress].userAge = userAge;
       userStructs[userAddress].userName = userName;
       userStructs[userAddress].userEmail = userEmail;
       userStructs[userAddress].index = userIndex.push(userAddress) - 1; //error in this line
}

我從一篇關於 Solidity CRUD 的文章中得到了這段程式碼,並且正在做一些修改,但我遇到瞭如下錯誤:

TypeError:類型 tuple() 不能隱式轉換為預期的 uint256 類型。使用者結構

$$ userAddress $$.index = userIndex.push(userAddress) - 1;

問題是由於 solc v0.6.0userIndex.push(userAddress)不再像以前的版本那樣返回長度。

你可以用這樣的東西修復程式碼

userIndex.push(userAddress);
userStructs[userAddress].index = userIndex.length - 1;

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