current position:Home>The voting code runs on Remix. The constructor version is compatible. The code and error information are attached. Please have a look.

The voting code runs on Remix. The constructor version is compatible. The code and error information are attached. Please have a look.

2022-02-03 20:53:16 Q & A of Denglian community

pragma solidity ^0.4.21;pragma experimental ABIEncoderV2;contract VoteContract{    // Voters     struct Voter{        // Voting number          uint voteNumber;        // Have you ever voted , You have already voted and are not allowed to vote again         bool isVoted;        // Voting weight , When the initial for 1, After being entrusted , Weight increase         uint weight;        //  The agent          address delegate;    }    // The candidate     struct Candidate{        // name         string name;        // Get the number of votes         uint voteCout;    }    // The administrator is responsible for creating the contract , Authorized to address , Make it a voter     address public admin;    // Candidate array       Candidate[] public candidates;    // The voters gathered     mapping(address=>Voter) public Voters;    // Constructors     constructor(string[] candidateNames)public{        // Obtain this contract         admin=msg.sender;        // Each name generates a candidate , Add to candidate collection         //memory Memory variable         for(uint i=0;i<candidateNames.length;i++){            Candidate memory tmp=Candidate({name:candidateNames[i],voteCout:0});            candidates.push(tmp);        }    }    // Limit that only administrators have the right to add voters     modifier adminOnly(){        require(admin==msg.sender);        _;    }    // Add voter     function giveVoteRightTo(address addr) adminOnly public{        if(voters[addr].weight>0)            revert();        voters[addr].weight=1;    }     // vote     function vote(uint voteNum) public{        Voter memory voter = voters[msg.sender];        // Not a candidate or has voted , Go straight back to         if(voter.weight<=0 || voter.isVoted)          revert();        voter.isVoted=true;        voter.voteNum=voteNum;        // The candidate's number and storage location have been , The first 0 Candidates are stored in the 0 A place         candidates[voteNum].voteCout+=voter.weight;    }    function delegateFunc() public{        voter storage voter = voters[msg.sender];        // Not a candidate or has voted , Go straight back to         if(voter.weight<=0||voter.isVoted)        revert();        // If the agent also appoints an agent , Then you need to poll to find the final agent .        while(voters[to].delegate!=address(0)&&voters[to].delegate!=msg.sender){            to=voters[to].delegate;        }        // The agent doesn't quit by himself         require(msg.sender!=to);        if(msg.sender==to){            revert();        }        voter.isVoted=true;        voter.delegate=to;        Voter storage FinalDelegateVoter= voters[to];        if(finalDelegateVoter.isVoted){             // If the proxy has voted , Then add your own weight to the number of candidates voting by proxy             candidates[finalDelegateVoter.voteNum].voteCout+=voter.weight;        }else{            // otherwise , Add your own weight to the agent's weight             finalDelegateVoter.weight+=voter.weight;        }    }    function whoWin() view public returns (string ,uint){        string winner;        uint winerVoteCount;        for(uint i=0; i<candidates.length; i++){            if(candidates[i].voteCout>winerVoteCount){                winerVoteCoun=candidates[i].voteCout;                winner=candidates[i].name;            }        }        return (winner,winerVoteCount);    }}

image.png




Take the answer 1:

You specified on the first line 0.4 Version of compiler : pragma solidity ^0.4.21;

0.4 The constructor of the version is as follows :

function VoteContract(string[] candidateNames) public

and constructor(string[] candidateNames)public yes 0.5 After the grammar . It is recommended to upgrade the compiler ,Solidity Syntax allows you to view documents : https://learnblockchain.cn/docs/solidity/contracts.html#constructor




copyright notice
author[Q & A of Denglian community],Please bring the original link to reprint, thank you.
https://en.netfreeman.com/2022/02/202202032053127129.html

Random recommended