0

This is my code:

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);
}

}

I am facing a problem here:

function getNameCandidate() public view returns(uint){
    returns Candidate.length;
}

it's showing me this error:

browser/Test.sol:38:5: ParserError: Expected primary expression.
    returns Candidate.length;
    ^-----^

How do I solve this problem? Thanks in advance.

2
  • 1
    When you return a value, you use the singular return not returns. Commented Dec 4, 2018 at 6:27
  • I changed returns to return and now I got two errors: function Election(string _name) public { owner = msg.sender; electionName = _name; } function addCanditate(string _name) ownerOnly public { candidates.push(Candidate(_name, 0)); } in tese functions
    – Abbas
    Commented Dec 4, 2018 at 9:17

3 Answers 3

1

In your Contract code after correct returns to return there are lots of errors.

Just refer this code for changes where you face error in your code and you can freely ask questions regarding any changes in a comment if you don't get it.

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); }

}
6
  • thanks, bro everything is okay now but when I want to deploy this contract I got one error: creation of Election errored: Error encoding arguments: SyntaxError: Unexpected token h in JSON at position 1
    – Abbas
    Commented Dec 5, 2018 at 5:53
  • No brother, I copied my code and paste on remix and it is deployed successfully without any error. If possible then copy my code and paste as it is and select compiler version version:0.5.0+commit.1d4f565a.Emscripten.clang. Commented Dec 5, 2018 at 6:01
  • I want to give a name for this election like "best student in the class"
    – Abbas
    Commented Dec 5, 2018 at 8:08
  • [![enter image description here][1]][1] [1]: i.sstatic.net/jI96x.png I want to give a name to this election like "Best student in the class" so I got this error: [![enter image description here][1]][1] [1]: i.sstatic.net/yM2Su.png
    – Abbas
    Commented Dec 5, 2018 at 8:13
  • You entering string value and remember remix always take string with in " ". so you have to enter Best student in the class as "Best student in the class". for more check image [i.sstatic.net/7K67E.png] Commented Dec 5, 2018 at 8:56
1

You have multiple errors in your code. First, as per the @shawn's comment, for returning any value, we use return not returns like other languages. Also a struct doesn't have a member length. So if you are looking to the get the length of candidates array. You should write it as:

function getNameCandidate() public view returns(uint){
    return candidates.length;
}
2
  • I changed returns to return and now I got two errors: function Election(string _name) public { owner = msg.sender; electionName = _name; } function addCanditate(string _name) ownerOnly public { candidates.push(Candidate(_name, 0)); } in tese functions
    – Abbas
    Commented Dec 4, 2018 at 9:14
  • Please update the question with new errors
    – Aniket
    Commented Dec 4, 2018 at 10:04
0

So with the new versions of solidity the constructor is not defined as a function with the same name as the contract anymore. Define the following function as follows:

This function:

function Election(string _name) public { owner = msg.sender; electionName = _name; } should be declared as: constructor(string _name) public { owner = msg.sender; electionName = _name; }

Also in the newer versions of solidity you need to define where some variables will be stored. Thus just add memory to the next function as indicated below:

function addCanditate(string memory _name) ownerOnly public { candidates.push(Candidate(_name, 0)); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.