如何解决固态声明错误?


`//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代码错误信息

我将其定义为变量,但这不起作用&给我一个解决办法。请给我解决这个错误的方法。

p。有扎实编程或区块链开发经验的S- DO评论

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

在顶部,你声明了:

Counters.Counter public _votingId;

但是试图呼叫_voterId

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

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

相关内容