将数据推入嵌套结构数组

发布于 2025-02-06 07:46:35 字数 5898 浏览 2 评论 0原文

我试图将项目推入候选人 选举内的数组 struct struct,但我的编译器正在返回错误。

我该如何解决?我的代码是;

//SPDX-License-Identifier: MIT;

pragma solidity ^0.8.0;

interface IERC20{
    function balanceOf(address owner) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address from,address to,uint256 amount) external returns (bool);
    event Transfer(address indexed from, address indexed to, uint256 value);
}

interface IERC721{
    function balanceOf(address owner) external view returns (uint256);
    function setApprovalForAll(address operator, bool _approved) external;
    function safeTransferFrom(address from,address to,uint256 tokenId) external;
    event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
}

contract Vote  {
    constructor (){}

    uint public candidateId = 0;
    uint public electionId= 0;
    mapping (uint => Election) allElections;
    mapping(uint => Candidate) allCandidates;
    mapping(uint => mapping(address => bool)) public voted;
    string public tie;
    


    struct Election {
        uint electionId;
        address creator;
        address identifier;
        string details;
        bool active;
        Candidate [] candidates
      
        }

    struct Candidate{
        uint electionId;
        uint candidateId;
        string name;
        uint vote;
    }

    function setUp( address _identifier, string memory _details , string[] calldata _candidate) public {  
         
       electionId++;
       allElections[electionId] = Election(
                                            electionId,
                                            msg.sender,
                                            _identifier,
                                            _details,
                                            false
                                            candidates[],
                                            );
        

        for(uint i = 0; i < _candidate.length; i++){
            candidateId++;
            Candidate memory candidate = Candidate(electionId,candidateId,_candidate[i],0);
            allCandidates[candidateId] = candidate;
            Election.candidates.push(candidate)

        }
    }

    function start(uint _electionId) public {
        require(allElections[_electionId].creator == msg.sender, "only moderator can start an election");
        allElections[_electionId].active = true;
           }
    
    function vote(uint _candidateId,uint _electionId ) public {
        require(voted[_electionId][msg.sender] == false, "you have already voted");
        require(allElections[_electionId].active == true, "election have not begun");
        address identifier = allElections[_electionId].identifier;
        require(IERC20(identifier).balanceOf(msg.sender) > 0 || IERC721(identifier).balanceOf(msg.sender) > 0,"only registered voters can vote");

        allCandidates[_candidateId].vote++;


        voted[_electionId][msg.sender] = true;
    }
    
    function announce(uint _electionId) public  returns (Candidate[] memory,string memory) {
        require(allElections[_electionId].creator == msg.sender, "only moderators can announce winner");
        allElections[_electionId].active = false;

        Candidate[] memory contestants = new Candidate[](candidateId);
        uint winningVoteCount = 0;
        uint256 winnerId;
        uint winningCandidateIndex = 0;
        for(uint i =0; i < candidateId ; i++){
            if(allCandidates[i + 1].electionId == _electionId){
                if (allCandidates[i + 1].vote > winningVoteCount){
                     winningVoteCount = allCandidates[i + 1].vote;
                     uint currentId = allCandidates[i + 1].candidateId;
                        winnerId= currentId;

                      Candidate storage currentItem = allCandidates[currentId];
                    contestants[winningCandidateIndex] = currentItem;
                    winningCandidateIndex += 1;
                        tie="";
                }else if(allCandidates[i + 1].vote == winningVoteCount){
                    tie = "This ended in a tie";
                }
                
            }
        }
       
    return (contestants, tie);
}

function booth() public view returns (Election[] memory){

         uint currentIndex = 0;
         uint total = electionId;

        Election[] memory items = new Election[](total );

        /// @notice Loop through all items ever created
        for (
            uint i = 0;
            i < electionId;
            i++) {
            
            /// @notice Get only public item
            if (allElections[i + 1].active == true) {
                uint currentId = allElections[i + 1].electionId;
                Election storage currentItem = allElections[currentId];
                items[currentIndex] = currentItem;
                currentIndex += 1;
            }
        }
        return items;
   
    }

function myElections() public view returns (Election[] memory){

         uint currentIndex = 0;
         uint total = electionId;

        Election[] memory items = new Election[](total);

        /// @notice Loop through all items ever created
        for (
            uint i = 0;
            i < electionId;
            i++) {
            
            /// @notice Get only public item
            if (allElections[i + 1].creator == msg.sender) {
                uint currentId = allElections[i + 1].electionId;
                Election storage currentItem = allElections[currentId];
                items[currentIndex] = currentItem;
                currentIndex += 1;
            }
        }
        return items;
   
    }
}

I am trying to push items into the candidates array inside the Election struct but my compiler is returning errors.

how can I solve this? my code is;

//SPDX-License-Identifier: MIT;

pragma solidity ^0.8.0;

interface IERC20{
    function balanceOf(address owner) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address from,address to,uint256 amount) external returns (bool);
    event Transfer(address indexed from, address indexed to, uint256 value);
}

interface IERC721{
    function balanceOf(address owner) external view returns (uint256);
    function setApprovalForAll(address operator, bool _approved) external;
    function safeTransferFrom(address from,address to,uint256 tokenId) external;
    event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
}

contract Vote  {
    constructor (){}

    uint public candidateId = 0;
    uint public electionId= 0;
    mapping (uint => Election) allElections;
    mapping(uint => Candidate) allCandidates;
    mapping(uint => mapping(address => bool)) public voted;
    string public tie;
    


    struct Election {
        uint electionId;
        address creator;
        address identifier;
        string details;
        bool active;
        Candidate [] candidates
      
        }

    struct Candidate{
        uint electionId;
        uint candidateId;
        string name;
        uint vote;
    }

    function setUp( address _identifier, string memory _details , string[] calldata _candidate) public {  
         
       electionId++;
       allElections[electionId] = Election(
                                            electionId,
                                            msg.sender,
                                            _identifier,
                                            _details,
                                            false
                                            candidates[],
                                            );
        

        for(uint i = 0; i < _candidate.length; i++){
            candidateId++;
            Candidate memory candidate = Candidate(electionId,candidateId,_candidate[i],0);
            allCandidates[candidateId] = candidate;
            Election.candidates.push(candidate)

        }
    }

    function start(uint _electionId) public {
        require(allElections[_electionId].creator == msg.sender, "only moderator can start an election");
        allElections[_electionId].active = true;
           }
    
    function vote(uint _candidateId,uint _electionId ) public {
        require(voted[_electionId][msg.sender] == false, "you have already voted");
        require(allElections[_electionId].active == true, "election have not begun");
        address identifier = allElections[_electionId].identifier;
        require(IERC20(identifier).balanceOf(msg.sender) > 0 || IERC721(identifier).balanceOf(msg.sender) > 0,"only registered voters can vote");

        allCandidates[_candidateId].vote++;


        voted[_electionId][msg.sender] = true;
    }
    
    function announce(uint _electionId) public  returns (Candidate[] memory,string memory) {
        require(allElections[_electionId].creator == msg.sender, "only moderators can announce winner");
        allElections[_electionId].active = false;

        Candidate[] memory contestants = new Candidate[](candidateId);
        uint winningVoteCount = 0;
        uint256 winnerId;
        uint winningCandidateIndex = 0;
        for(uint i =0; i < candidateId ; i++){
            if(allCandidates[i + 1].electionId == _electionId){
                if (allCandidates[i + 1].vote > winningVoteCount){
                     winningVoteCount = allCandidates[i + 1].vote;
                     uint currentId = allCandidates[i + 1].candidateId;
                        winnerId= currentId;

                      Candidate storage currentItem = allCandidates[currentId];
                    contestants[winningCandidateIndex] = currentItem;
                    winningCandidateIndex += 1;
                        tie="";
                }else if(allCandidates[i + 1].vote == winningVoteCount){
                    tie = "This ended in a tie";
                }
                
            }
        }
       
    return (contestants, tie);
}

function booth() public view returns (Election[] memory){

         uint currentIndex = 0;
         uint total = electionId;

        Election[] memory items = new Election[](total );

        /// @notice Loop through all items ever created
        for (
            uint i = 0;
            i < electionId;
            i++) {
            
            /// @notice Get only public item
            if (allElections[i + 1].active == true) {
                uint currentId = allElections[i + 1].electionId;
                Election storage currentItem = allElections[currentId];
                items[currentIndex] = currentItem;
                currentIndex += 1;
            }
        }
        return items;
   
    }

function myElections() public view returns (Election[] memory){

         uint currentIndex = 0;
         uint total = electionId;

        Election[] memory items = new Election[](total);

        /// @notice Loop through all items ever created
        for (
            uint i = 0;
            i < electionId;
            i++) {
            
            /// @notice Get only public item
            if (allElections[i + 1].creator == msg.sender) {
                uint currentId = allElections[i + 1].electionId;
                Election storage currentItem = allElections[currentId];
                items[currentIndex] = currentItem;
                currentIndex += 1;
            }
        }
        return items;
   
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿光 2025-02-13 07:46:35

我从详细信息函数上调整了您的智能合约代码。我写了一些笔记,以使您了解错误以及如何解决该错误。
您可以在此行下方看到代码:

//SPDX-License-Identifier: MIT;
pragma solidity ^0.8.0;

interface IERC20{
    function balanceOf(address owner) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address from,address to,uint256 amount) external returns (bool);
    event Transfer(address indexed from, address indexed to, uint256 value);
}

interface IERC721{
    function balanceOf(address owner) external view returns (uint256);
    function setApprovalForAll(address operator, bool _approved) external;
    function safeTransferFrom(address from,address to,uint256 tokenId) external;
    event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
}

contract Vote  {
    constructor (){}

    uint public candidateId = 0;
    uint public electionId= 0;
    mapping (uint => Election) public allElections;
    mapping(uint => Candidate) allCandidates;
    mapping(uint => mapping(address => bool)) public voted;
    string public tie;
    


    struct Election {
        uint electionId;
        address creator;
        address identifier;
        string details;
        bool active;
        Candidate[] candidates;
    }

    struct Candidate{
        uint electionId;
        uint candidateId;
        string name;
        uint vote;
    }

    function setUp( address _identifier, string memory _details , string[] calldata _candidate) public {  
        electionId++;
        // NOTE: I retrieve an empty election struct from mapping and set for each field the values.  
        Election storage election = allElections[electionId];
        election.electionId = electionId;
        election.creator = msg.sender;
        election.identifier = _identifier;
        election.details = _details;
        election.active = true;

        for(uint i = 0; i < _candidate.length; i++){
            candidateId++;
            Candidate memory candidate = Candidate(electionId,candidateId,_candidate[i],0);
            allCandidates[candidateId] = candidate;
            // NOTE: For insert the candidates inside the same election struct, I use election struct retrieved in line 51 and 
            //       then use 'push' method for add single candidate
            election.candidates.push(candidate);
        }
    }

    function start(uint _electionId) public {
        require(allElections[_electionId].creator == msg.sender, "only moderator can start an election");
        allElections[_electionId].active = true;
           }
    
    function vote(uint _candidateId,uint _electionId ) public {
        require(voted[_electionId][msg.sender] == false, "you have already voted");
        require(allElections[_electionId].active == true, "election have not begun");
        address identifier = allElections[_electionId].identifier;
        require(IERC20(identifier).balanceOf(msg.sender) > 0 || IERC721(identifier).balanceOf(msg.sender) > 0,"only registered voters can vote");

        allCandidates[_candidateId].vote++;


        voted[_electionId][msg.sender] = true;
    }
    
    function announce(uint _electionId) public  returns (Candidate[] memory,string memory) {
        require(allElections[_electionId].creator == msg.sender, "only moderators can announce winner");
        allElections[_electionId].active = false;

        Candidate[] memory contestants = new Candidate[](candidateId);
        uint winningVoteCount = 0;
        uint256 winnerId;
        uint winningCandidateIndex = 0;
        for(uint i =0; i < candidateId ; i++){
            if(allCandidates[i + 1].electionId == _electionId){
                if (allCandidates[i + 1].vote > winningVoteCount){
                     winningVoteCount = allCandidates[i + 1].vote;
                     uint currentId = allCandidates[i + 1].candidateId;
                        winnerId= currentId;

                      Candidate storage currentItem = allCandidates[currentId];
                    contestants[winningCandidateIndex] = currentItem;
                    winningCandidateIndex += 1;
                        tie="";
                }else if(allCandidates[i + 1].vote == winningVoteCount){
                    tie = "This ended in a tie";
                }
                
            }
        }
       
    return (contestants, tie);
}

function booth() public view returns (Election[] memory){

         uint currentIndex = 0;
         uint total = electionId;

        Election[] memory items = new Election[](total );

        /// @notice Loop through all items ever created
        for (
            uint i = 0;
            i < electionId;
            i++) {
            
            /// @notice Get only public item
            if (allElections[i + 1].active == true) {
                uint currentId = allElections[i + 1].electionId;
                Election storage currentItem = allElections[currentId];
                items[currentIndex] = currentItem;
                currentIndex += 1;
            }
        }
        return items;
   
    }

function myElections() public view returns (Election[] memory){

         uint currentIndex = 0;
         uint total = electionId;

        Election[] memory items = new Election[](total);

        /// @notice Loop through all items ever created
        for (
            uint i = 0;
            i < electionId;
            i++) {
            
            /// @notice Get only public item
            if (allElections[i + 1].creator == msg.sender) {
                uint currentId = allElections[i + 1].electionId;
                Election storage currentItem = allElections[currentId];
                items[currentIndex] = currentItem;
                currentIndex += 1;
            }
        }
        return items;
   
    }
}

I adjusted your smart contract code, in details setUp function. I put some notes for make understand you the error and how to solve it.
You can see the code below this line:

//SPDX-License-Identifier: MIT;
pragma solidity ^0.8.0;

interface IERC20{
    function balanceOf(address owner) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address from,address to,uint256 amount) external returns (bool);
    event Transfer(address indexed from, address indexed to, uint256 value);
}

interface IERC721{
    function balanceOf(address owner) external view returns (uint256);
    function setApprovalForAll(address operator, bool _approved) external;
    function safeTransferFrom(address from,address to,uint256 tokenId) external;
    event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
}

contract Vote  {
    constructor (){}

    uint public candidateId = 0;
    uint public electionId= 0;
    mapping (uint => Election) public allElections;
    mapping(uint => Candidate) allCandidates;
    mapping(uint => mapping(address => bool)) public voted;
    string public tie;
    


    struct Election {
        uint electionId;
        address creator;
        address identifier;
        string details;
        bool active;
        Candidate[] candidates;
    }

    struct Candidate{
        uint electionId;
        uint candidateId;
        string name;
        uint vote;
    }

    function setUp( address _identifier, string memory _details , string[] calldata _candidate) public {  
        electionId++;
        // NOTE: I retrieve an empty election struct from mapping and set for each field the values.  
        Election storage election = allElections[electionId];
        election.electionId = electionId;
        election.creator = msg.sender;
        election.identifier = _identifier;
        election.details = _details;
        election.active = true;

        for(uint i = 0; i < _candidate.length; i++){
            candidateId++;
            Candidate memory candidate = Candidate(electionId,candidateId,_candidate[i],0);
            allCandidates[candidateId] = candidate;
            // NOTE: For insert the candidates inside the same election struct, I use election struct retrieved in line 51 and 
            //       then use 'push' method for add single candidate
            election.candidates.push(candidate);
        }
    }

    function start(uint _electionId) public {
        require(allElections[_electionId].creator == msg.sender, "only moderator can start an election");
        allElections[_electionId].active = true;
           }
    
    function vote(uint _candidateId,uint _electionId ) public {
        require(voted[_electionId][msg.sender] == false, "you have already voted");
        require(allElections[_electionId].active == true, "election have not begun");
        address identifier = allElections[_electionId].identifier;
        require(IERC20(identifier).balanceOf(msg.sender) > 0 || IERC721(identifier).balanceOf(msg.sender) > 0,"only registered voters can vote");

        allCandidates[_candidateId].vote++;


        voted[_electionId][msg.sender] = true;
    }
    
    function announce(uint _electionId) public  returns (Candidate[] memory,string memory) {
        require(allElections[_electionId].creator == msg.sender, "only moderators can announce winner");
        allElections[_electionId].active = false;

        Candidate[] memory contestants = new Candidate[](candidateId);
        uint winningVoteCount = 0;
        uint256 winnerId;
        uint winningCandidateIndex = 0;
        for(uint i =0; i < candidateId ; i++){
            if(allCandidates[i + 1].electionId == _electionId){
                if (allCandidates[i + 1].vote > winningVoteCount){
                     winningVoteCount = allCandidates[i + 1].vote;
                     uint currentId = allCandidates[i + 1].candidateId;
                        winnerId= currentId;

                      Candidate storage currentItem = allCandidates[currentId];
                    contestants[winningCandidateIndex] = currentItem;
                    winningCandidateIndex += 1;
                        tie="";
                }else if(allCandidates[i + 1].vote == winningVoteCount){
                    tie = "This ended in a tie";
                }
                
            }
        }
       
    return (contestants, tie);
}

function booth() public view returns (Election[] memory){

         uint currentIndex = 0;
         uint total = electionId;

        Election[] memory items = new Election[](total );

        /// @notice Loop through all items ever created
        for (
            uint i = 0;
            i < electionId;
            i++) {
            
            /// @notice Get only public item
            if (allElections[i + 1].active == true) {
                uint currentId = allElections[i + 1].electionId;
                Election storage currentItem = allElections[currentId];
                items[currentIndex] = currentItem;
                currentIndex += 1;
            }
        }
        return items;
   
    }

function myElections() public view returns (Election[] memory){

         uint currentIndex = 0;
         uint total = electionId;

        Election[] memory items = new Election[](total);

        /// @notice Loop through all items ever created
        for (
            uint i = 0;
            i < electionId;
            i++) {
            
            /// @notice Get only public item
            if (allElections[i + 1].creator == msg.sender) {
                uint currentId = allElections[i + 1].electionId;
                Election storage currentItem = allElections[currentId];
                items[currentIndex] = currentItem;
                currentIndex += 1;
            }
        }
        return items;
   
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文