- Entrociter[_entrociterID].vots++///是在哪里";应为整数常数";被抛出。这应该允许用户使用他们的id进行投票///
'''
pragma solidity >=0.6.0;
contract Newjudge {
uint public constant MAX_VOTES_PER_VOTER= 1;
struct Entrociter {
uint id;
string name;
string party;
uint votes;
}
event Voted ();
event NewEntrociter ();
mapping(uint => Entrociter) public entrociters;
uint public entrociterCount;
mapping(address => uint) public votes;
constructor() {
entrociterCount = 0;
}
function vote(uint _entrociterID) public {
require(votes[msg.sender] <MAX_VOTES_PER_VOTER, "Voter has no votes left.");
require(_entrociterID > 0 && _entrociterID <= entrociterCount, " Entrociter ID is out of range.");
votes[msg.sender]++;
Entrociter[_entrociterID].votes++;
Entrociter[entrociterCount] = Entrociter;
emit Voted();
}
function addEntrociter(string memory _name, string memory _party) public {
entrociterCount++;
Entrociter memory entrociter = Entrociter(entrociterCount, _name, _party, 0);
entrociter[entrociterCount] = entrociter;
emit NewEntrociter();
votes(entrociterCount);
}
}
''
您会得到这个错误"应为整数常数";因为CCD_ 1是一个类型而不是映射,所以不能有这个CCD_。应该是entrociters[_entrociterID]
。但是你会有一个参考错误。
因此,为了解决这个问题,因为你想更新存储变量,你可以获得它的引用,告诉EVM你正在更新该变量
function vote(uint _entrociterID) public {
require(votes[msg.sender] <MAX_VOTES_PER_VOTER, "Voter has no votes left.");
require(_entrociterID > 0 && _entrociterID <= entrociterCount, " Entrociter ID is out of range.");
votes[msg.sender]++;
// get the reference of struct that you want to update
Entrociter storage entrociter=entrociters[_entrociterID];
entrociter.votes++;
entrociters[entrociterCount] = entrociter;
emit Voted(); }
function addEntrociter(string memory _name, string memory _party) public {
entrociterCount++;
Entrociter memory entrociter = Entrociter(entrociterCount, _name, _party, 0);
entrociters[entrociterCount] = entrociter;
emit NewEntrociter();
// TypeError: Type is not callable
// votes(entrociterCount);
}