Este es mi código:
pragma solidity>0.4.99<0.6.0;
contract Election {
struct Candidate {
string name;
uint voteCount;
}
struct Voter {
bool authorized;
bool voted;
uint vote;
}
address public owner;
string public electionName;
mapping(address => Voter) public voters;
Candidate[] public candidates;
uint public totalVotes;
modifier ownerOnly(){
require(msg.sender == owner);
_;
}
function Election(string _name) public {
owner = msg.sender;
electionName = _name;
}
function addCanditate(string _name) ownerOnly public {
candidates.push(Candidate(_name, 0));
}
function getNameCandidate() public view returns(uint){
returns Candidate.length;
}
function authorize (address _person) ownerOnly public {
voters[_person].authorized = true;
}
function vote (uint _voteIndex) public {
require(!voters[msg.sender].voted);
require(voters[msg.sender].authorized);
voters[msg.sender].vote = _voteIndex;
voters[msg.sender].voted = true;
Candidates[_voteIndex].voteCount += 1;
totalVotes += 1;
}
function end() ownerOnly public {
selfdestruct(owner);
}
}
Estoy enfrentando un problema aquí:
function getNameCandidate() public view returns(uint){
returns Candidate.length;
}
me esta mostrando este error:
browser/Test.sol:38:5: ParserError: Expected primary expression.
returns Candidate.length;
^-----^
¿Cómo soluciono este problema? Gracias de antemano.
En su código de contrato después de corregir returns
hay return
muchos errores.
Simplemente consulte este código para ver los cambios en los que enfrenta un error en su código y puede hacer preguntas libremente sobre cualquier cambio en un comentario si no lo recibe.
pragma solidity>0.4.99<0.6.0;
contract Election {
struct Candidate {
string name;
uint voteCount; }
struct Voter {
bool authorized;
bool voted;
uint vote; }
address public owner; string public electionName;
mapping(address => Voter) public voters; Candidate[] public candidates; uint public totalVotes;
modifier ownerOnly(){
require(msg.sender == owner);
_; }
constructor (string memory _name) public {
owner = msg.sender;
electionName = _name; }
function addCanditate(string memory _name) ownerOnly public {
candidates.push(Candidate(_name, 0)); }
function getNameCandidate() public view returns(uint){
return candidates.length; }
function authorize (address _person) ownerOnly public {
voters[_person].authorized = true; }
function vote (uint _voteIndex) public {
require(!voters[msg.sender].voted);
require(voters[msg.sender].authorized);
voters[msg.sender].vote = _voteIndex;
voters[msg.sender].voted = true;
candidates[_voteIndex].voteCount += 1;
totalVotes += 1; } function end() ownerOnly public {
selfdestruct(msg.sender); }
}
version:0.5.0+commit.1d4f565a.Emscripten.clang
." "
. por lo que debe ingresar Best student in the class
como "Mejor alumno de la clase" . para ver más imágenes [ i.stack.imgur.com/7K67E.png]Tienes varios errores en tu código. Primero, según el comentario de @shawn, para devolver cualquier valor, return
no usamos returns
otros idiomas. Además, a struct
no tiene un miembro length
. Entonces, si está buscando obtener la longitud de la candidates
matriz. Debes escribirlo como:
function getNameCandidate() public view returns(uint){
return candidates.length;
}
Entonces, con las nuevas versiones de solidity, el constructor ya no se define como una función con el mismo nombre que el contrato. Defina la siguiente función de la siguiente manera:
Esta función:
function Elección (string _name) public { propietario = msg.sender; elecciónNombre = _nombre; } debe declararse como: constructor(string _name) public { propietario = msg.sender; elecciónNombre = _nombre; }
Además, en las versiones más nuevas de solidity, debe definir dónde se almacenarán algunas variables. Por lo tanto, simplemente agregue memoria a la siguiente función como se indica a continuación:
function addCanditate(string memory _name) ownOnly public { candidatos.push(Candidate(_name, 0)); }
shawn tabrizi
return
notreturns
.Abbas