0

Cloned a project from a tutorial from here which is a voting dapp on etherum https://github.com/dappuniversity/election. When i try to compile the contract i get the following error

project/blockvote/contracts/Migrations.sol:20:5: TypeError: Name has to refer to a struct, enum or contract.

Migrations upgraded = Migrations(new_address);

I have tried to fix the problem with no luck tried changing the Election to election and tried adding the constructor because of the new solidity ersion with no luck. Also tried changing the prgama solditiy version and still get errors

pragma solidity ^0.5.0;

contract elections {
    // model a candidate
    // this is basically describe about how our candidate looks like (has a name, a id and vote count)
    struct Candidate {
        uint id;
        string name;
        uint voteCount;
    }

    // storing the accounts that have already voted
    mapping(address=>bool) public voters;
    // store a candidate
    mapping(uint=>Candidate) public candidates; // creating the variable of that mapping
    // fetch candidate 

    // get candidate count
    uint public candidatesCount; // this is the counter

    // voting event 
    event votedEvent (
        uint indexed _candidateId
    );

    constructor() public {
        // we dont want any one else to add the candidates therefore we call the addCandidate function in the constructor
        addCandidates("Candidate 1");
        addCandidates("Candidate 2");
        addCandidates("Candidate 3");
        // candidates count is 3
    }
    // adding the candidate to the mapping
    function addCandidates(string memory _name) private {
        candidatesCount++; // this will increment the candidate counter
        candidates[candidatesCount] = Candidate(candidatesCount, _name, 0); // assiging value to the candidate structure
    }

    function vote(uint _candidateId) public {
        //check that the address has not voted before
        require(!voters[msg.sender]);

        // require a valid candidate
        require(_candidateId > 0 && _candidateId <= candidatesCount);
        // record that voter has voted
        voters[msg.sender] = true; // msg.sender has the account of the voter

        // update candidate vote count
        candidates[_candidateId].voteCount++;

        emit votedEvent(_candidateId);
    }
}
pragma solidity ^0.4.2;

contract Migrations {
    address public owner;
    uint public last_completed_migration;

    modifier restricted() {
        if (msg.sender == owner) _;
    }

    function Migrations() {
        owner = msg.sender;
    }

    function setCompleted(uint completed) restricted {
        last_completed_migration = completed;
    }

    function upgrade(address new_address) restricted {
        Migrations upgraded = Migrations(new_address);
        upgraded.setCompleted(last_completed_migration);
    }
}
  • 1
    for line 'mapping(uint=>Candidate) public candidates;' Candidate is a type, I think u r missing the code for this, if not please update your question with the code. Basically from the error seems u r missing a type 'Candidate' – Nigel Savage Feb 17 at 19:26
  • I reformatted my code hope this helps. This code hade worked for me before with no issues i think some update caused these errorss – stacksonoverflow Feb 18 at 3:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.