1
//一个投票的合约
contract Ballot{
    
    //提案对象
    struct Proposal{
        bytes32 name;
        uint voteCount;//累计票数
    }

    
    //获取获胜的对象
    function winningProposal() public view returns (Proposal[] memory)
    {
        Proposal[] memory win;
        uint winningVoteCount = 0;
        for(uint i=0;i < proposals.length;i++){
            if(proposals[i].voteCount > winningVoteCount){
                winningVoteCount = proposals[i].voteCount;
            }
        }

        for(uint m=0; m < proposals.length; m++){
            if(winningVoteCount == proposals[m].voteCount){
                // push can't use
                win.push(proposals[m]);
                // next line can use
                // win[win.length] = proposals[m];
            }
        }
        

        return win;
    }
}


enter image description here

0

1 Answer 1

2

From https://docs.soliditylang.org/en/v0.8.16/types.html?highlight=Push#allocating-memory-arrays

As opposed to storage arrays, it is not possible to resize memory arrays (e.g. the .push member functions are not available).

Not the answer you're looking for? Browse other questions tagged or ask your own question.