投票代码在Remix上运行构造函数版本兼容问题,附代码和报错信息,请有心帮忙看看。

pragma solidity ^0.4.21;
pragma experimental ABIEncoderV2;

contract VoteContract{
    //投票人
    struct Voter{
        //投票编号 
        uint voteNumber;

        //是否投过票,已经投过不允许再次投票
        bool isVoted;

        //投票权重,初始时为1,被委托后,权重增加
        uint weight;

        // 代理人 
        address delegate;
    }

    //候选人
    struct Candidate{
        //名字
        string name;
        //获得投票数
        uint voteCout;
    }

    //管理员负责创建合约,授权给地址,使之成为投票人
    address public admin;

    //候选人数组  
    Candidate[] public candidates;

    //投票人集合
    mapping(address=>Voter) public Voters;

    //构造函数
    constructor(string[] candidateNames)public{

        //获取本合约
        admin=msg.sender;

        //每一个名字都生成一个候选人,添加到候选人集合中
        //memory内存型变量
        for(uint i=0;i<candidateNames.length;i++){
            Candidate memory tmp=Candidate({name:candidateNames[i],voteCout:0});
            candidates.push(tmp);
        }

    }

    //限定只有管理员有添加投票人的权利
    modifier adminOnly(){
        require(admin==msg.sender);
        _;
    }

    //添加投票人
    function giveVoteRightTo(address addr) adminOnly public{
        if(voters[addr].weight>0)
            revert();
        voters[addr].weight=1;
    } 

    //投票
    function vote(uint voteNum) public{
        Voter memory voter = voters[msg.sender];

        //不是候选人或已经投过票,直接返回
        if(voter.weight<=0 || voter.isVoted)
          revert();

        voter.isVoted=true;
        voter.voteNum=voteNum;

        //候选人的编号和存储位置一直,第0个候选人存储在第0个位置
        candidates[voteNum].voteCout+=voter.weight;
    }

    function delegateFunc() public{
        voter storage voter = voters[msg.sender];
        //不是候选人或已经投过票,直接返回
        if(voter.weight<=0||voter.isVoted)
        revert();

        //如果代理人也指定了代理人,那么需要轮询找到最终代理人。
        while(voters[to].delegate!=address(0)&&voters[to].delegate!=msg.sender){
            to=voters[to].delegate;
        }

        //代理不是自己退出
        require(msg.sender!=to);
        if(msg.sender==to){
            revert();
        }

        voter.isVoted=true;
        voter.delegate=to;

        Voter storage FinalDelegateVoter= voters[to];

        if(finalDelegateVoter.isVoted){
             //如果代理人已经投票,那么在代理人投票的候选人票数上加上自己的权重
            candidates[finalDelegateVoter.voteNum].voteCout+=voter.weight;
        }else{
            //否则,在代理人权重上加上自己的权重
            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

请先 登录 后评论

最佳答案 2020-04-13 20:15

你在第一行指定了 0.4 版本的编译器: pragma solidity ^0.4.21;

0.4 版本的构造函数如下:

function VoteContract(string[] candidateNames) public

constructor(string[] candidateNames)public 是 0.5 之后的语法。 建议升级编译器,Solidity 语法可以查看文档: https://learnblockchain.cn/docs/solidity/contracts.html#constructor

请先 登录 后评论

其它 0 个回答