Solidity
使用數組數組作為函式參數返回“從 uint256 到 uint256 的無效隱式轉換請求記憶體。”
請考慮這份契約:
pragma solidity 0.8.7; contract Test { struct UserStruct { uint256 betAmount; uint256[] potentialWinnings; string teamChosen;} mapping(address => UserStruct[]) public userStructs; function appendUserBet(string memory userTeamChosen,uint256 eventNumber) public payable { UserStruct memory userStruct = UserStruct(msg.value,new uint256[](0),userTeamChosen); userStructs[msg.sender].push(userStruct);} function updatePotentialWinnings(uint256 eventNumber,address userId,uint256[] memory potentialWinnings) public { userStructs[userId][eventNumber].potentialWinnings = potentialWinnings;}}
它工作正常,但是當我在契約下麵包含一個函式時,它不會編譯。它出錯了
TypeError: Invalid type for argument in function call. Invalid implicit conversion from uint256 to uint256[] memory requested.
function addAllPotentialWinnings(uint256 _eventNumber, uint256[] memory _potentialWinnings, address[] memory _usersList) public { for (uint i =0; i<_usersList.length;i++){ updatePotentialWinnings(_eventNumber,_usersList[i],_potentialWinnings[i]);}}
我想提供一個數組數組(動態大小的二維數組)作為函式的參數,但我似乎做錯了。據我所知,它
UserStruct
期望potentialWinnings
採用uint256[]
格式,但它以uint256
. 按照設計,我想為函式提供一個如下所示的數組:[[0,1,2],[2,5,6],[x,y,z],...]
如何更改我的契約,使其“知道”我作為參數傳遞的數組將在裡麵包含所需的數組?
先感謝您!
只需使用二維數組
function addAllPotentialWinnings(uint256 _eventNumber, uint256[][] memory _potentialWinnings, address[] memory _usersList) public { for (uint i =0; i<_usersList.length;i++){ updatePotentialWinnings(_eventNumber,_usersList[i],_potentialWinnings[i]); } }