[Ethereum] solidity code compilation error

remixsoliditytruffle-contract

pragma solidity ^0.5.0;

contract Ballot {

    struct Voter {
        uint weight;
        bool voted;
        uint8 vote;
        // address delegate;
    }

    //modifer
    modifier onlyOwner () {
      require(msg.sender == chairperson);
      _;
    }

    /* struct Proposal {
        uint voteCount; // could add other data about proposal
    } */
    address public chairperson;
    mapping(address => Voter) public voters;
    uint[4] public proposals;

    // Create a new ballot with 4 different proposals.
    function Ballot() public {
        chairperson = msg.sender;
        voters[chairperson].weight = 2;
    }

    /// Give $(toVoter) the right to vote on this ballot.
    /// May only be called by $(chairperson).
    function register(address toVoter) public onlyOwner{
        if(voters[toVoter].weight != 0) revert();
        voters[toVoter].weight = 1;
        voters[toVoter].voted = false;
    }

    /// Give a single vote to proposal $(toProposal).
    function vote(uint8 toProposal) public {
        Voter storage sender = voters[msg.sender];
        if (sender.voted || toProposal >= 4 || sender.weight == 0) revert();
        sender.voted = true;
        sender.vote = toProposal;
        proposals[toProposal] += sender.weight;
    }

    function winningProposal() public view returns (uint8 _winningProposal) {
        uint256 winningVoteCount = 0;
        for (uint8 prop = 0; prop < 4; prop++)
            if (proposals[prop] > winningVoteCount) {
                winningVoteCount = proposals[prop];
                _winningProposal = prop;
            }
    }

    function getCount() public view returns (uint[8]) {
        return proposals;
    }
}

truffle compile

Compiling ./contracts/Ballot.sol...
Compiling ./contracts/Migrations.sol...

Compilation warnings encountered:

/home/bc/bheem/Ballot2/contracts/Ballot.sol:26:5: Warning: This declaration shadows an existing declaration.
    function Ballot() public {
    ^ (Relevant source part starts here and spans across multiple lines).
/home/bc/bheem/Ballot2/contracts/Ballot.sol:3:1: The shadowed declaration is here:
contract Ballot {
^ (Relevant source part starts here and spans across multiple lines).

,/home/bc/bheem/Ballot2/contracts/Migrations.sol:11:3: Warning: This declaration shadows an existing declaration.
  function Migrations() public {
  ^ (Relevant source part starts here and spans across multiple lines).
/home/bc/bheem/Ballot2/contracts/Migrations.sol:3:1: The shadowed declaration is here:
contract Migrations {
^ (Relevant source part starts here and spans across multiple lines).



/home/bc/bheem/Ballot2/contracts/Ballot.sol:26:5: SyntaxError: Functions are not allowed to have the same name as the contract. If you intend this to be a constructor, use "constructor(...) { ... }" to define it.
    function Ballot() public {
    ^ (Relevant source part starts here and spans across multiple lines).
,/home/bc/bheem/Ballot2/contracts/Migrations.sol:11:3: SyntaxError: Functions are not allowed to have the same name as the contract. If you intend this to be a constructor, use "constructor(...) { ... }" to define it.
  function Migrations() public {
  ^ (Relevant source part starts here and spans across multiple lines).
,/home/bc/bheem/Ballot2/contracts/Ballot.sol:26:5: Warning: This declaration shadows an existing declaration.
    function Ballot() public {
    ^ (Relevant source part starts here and spans across multiple lines).
/home/bc/bheem/Ballot2/contracts/Ballot.sol:3:1: The shadowed declaration is here:
contract Ballot {
^ (Relevant source part starts here and spans across multiple lines).

,/home/bc/bheem/Ballot2/contracts/Migrations.sol:11:3: Warning: This declaration shadows an existing declaration.
  function Migrations() public {
  ^ (Relevant source part starts here and spans across multiple lines).
/home/bc/bheem/Ballot2/contracts/Migrations.sol:3:1: The shadowed declaration is here:
contract Migrations {
^ (Relevant source part starts here and spans across multiple lines).

,/home/bc/bheem/Ballot2/contracts/Ballot.sol:57:46: TypeError: Data location must be "memory" for return parameter in function, but none was given.
    function getCount() public view returns (uint[8]) {
                                             ^-----^
Compilation failed. See above.
Truffle v5.0.2 (core: 5.0.2)
Node v8.12.0
 please help how to solve this ?

Best Answer

  1. You need to use constructor() to define constructor.

Replace

function Ballot() public

with

constructor() public 
  1. Pass index of array whose value you want in return. Do the iteration on application side and not in smart contract.

Replace

function getCount() public view returns (uint[8]) {
        return proposals;
    }

with

function getCount(uint index) public view returns (uint) {
        return proposals[index];
    }
Related Topic