繁体   English   中英

我怎样才能解决这个声明错误?

[英]how can i solve this declaration error in solidity?

`//SPDX-License-Identifier:Mit
pragma solidity ^0.8.9;

import "@openzeppelin/contracts/utils/Counters.sol";
import "hardhat/console.sol";

contract Create{
    using Counters for Counters.Counter;
    Counters.Counter public _votingId;
    Counters.Counter public _candidateId;
    address public votingOrganizer;
    //Candidate for voting
    struct Candidate{
        uint256 candidateId;
        uint age;
        string name;
        string image;
        uint256 voteCount;
        address _address;
        string ipfs;
    }

    event CandidateCreate(
        uint256 indexed candidateId,
        uint age,
        string name,
        string image,
        uint256 voteCount,
        address _address,
        string ipfs
    );
    address [] public candidateAddress;
    mapping (address => Candidate)public candidates;
    //end of candidate data

    //voter details
    address []public votedVoters;
    address []public votersAddress;
    mapping(address=>Voter)public voters;

    struct Voter{
        uint256 voter_voterId;
        string voter_name;
        string voter_image;
        address voter_address;
        uint256 voter_allowed;
        bool voter_voted;
        uint256 voter_vote;
        string voter_ipfs;
    }

    event VoterCreated(
        uint256 indexed voter_voterId,
        string voter_name,
        string voter_image,
        address voter_address,
        uint256 voter_allowed,
        bool voter_voted,
        uint256 voter_vote,
        string voter_ipfs

    );

//end of voter data
   
   constructor() {
    //to deploy contract
    votingOrganizer=msg.sender;
   }
   function setCandidate(address _address,string memory _age,string memory _name,
   string memory _image,string memory _ipfs)public{
    require(votingOrganizer==msg.sender,"only organizer can create candiate");
    _candidateId.increment();
    uint256 idNumber=_candidateId.current();

    Candidate storage candidate=candidates[_address];
    candidate.age=_age;
    candidate.name=_name;
    candidate.candidateId=idNumber;
    candidate.image=_image;
    candidate.voteCount=0;
    candidate._address=_address;
    candidate.ipfs=_ipfs;

    candidateAddress.push(_address);
    emit CandidateCreate(
        idNumber,
        _age,
        _name,
        _image,
        candidate.voteCount,
        _address,
        _ipfs
    );
   }

  //adress is state variable but we want copy of that why we taking memory
   function getCandidate() public view returns(address[] memory){
    return candidateAddress;
   }

//gives length of candidate
   function getCandidateLength() public view returns(uint256){
    return candidateAddress.length;
   }
///for getting user data
   function getCandidatedata(address _address) public view returns(string memory,string memory,
   uint256,string memory,uint256,string memory,address){
    return (
        candidates[_address].age,
        candidates[_address].name,
        candidates[_address].candidateId,
        candidates[_address].image,
        candidates[_address].voteCount,
        candidates[_address].ipfs,
        candidates[_address]._address
        );
   }
   
   ///-----voters section------------------------------------------------------------------------

   //to create voters
   function voterRight(address _address,string memory _name,
   string memory _image,string memory _ipfs)public{
    require(votingOrganizer==msg.sender,"only organizer can create voter");

    _voterId.increment();

    uint256 idNumber=_voterId.current();
    Voter storage voter = voters[_address];
    require(voter.voter_allowed==0);//user not registered for contest

    voter.voter_allowed=1;
    voter.voter_name=_name;
    voter.voter_image=_image;
    voter.voter_address=_address;
    voter.voter_voterId=idNumber;
    voter.voter_vote=1000;
    voter.voter_voted=false;
    voter.voter_ipfs=_ipfs;

    votersAddress.push(_address);

    emit VoterCreated(
        idNumber,
        _name,
        _image,
        _address,
        voter.voter_allowed,
        voter.voter_voted,
        voter.voter_vote,
        _ipfs
    );
   }

   function vote(address _candidateAddress,uint256 _candidateVoteId)external{
    Voter storage voter = voters[msg.sender];
    require(!voter.voter_voted,"you have alreday voted");
    require(voter.voter_allowed!=0,"no right to vote");

    voter.voter_voted = true;
    voter.voter_vote = _candidateVoteId;

    votedVoters.push(msg.sender);
    candidates[_candidateAddress].voteCount += voter.voter_allowed;
   }

   function getVoterLength() public view returns(uint256){
    return votersAddress.length;
   }
//get voters data
   function getVotersdata(address _address) public view returns(uint256,string memory,string memory,
   address,string memory,uint256,bool){
    return (
        voters[_address].voter_voterId,
        voters[_address].voter_name,
        voters[_address].voter_image,
        voters[_address].voter_address,
        voters[_address].voter_ipfs,
        voters[_address].voter_allowed,
        voters[_address].voter_voted
        );
   }

   //list of who has voted
   function getVotedVoterList() public view returns(address[] memory){
    return votedVoters;
   }
     //list of all voters
   function getVoterList() public view returns(address[] memory){
    return votersAddress;
   }
}`

请查看图像中的错误消息我正在使用 vs 代码错误消息中的 hardhat 编译 solidity 代码

我将其定义为变量但不起作用并给我解决方案。 请给我解决方案来摆脱这个错误。

PS-DO 评论谁有 solidity 编程或区块链开发经验。

您使用了错误的 state (变量)名称。

在顶部,您已声明:

    Counters.Counter public _votingId;

但试图打电话给_voterId

将您的代码更新为以下内容:

    _votingId.increment();
    uint256 idNumber=_votingId.current();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM