0
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 ?
3
  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];
    }
  • bc@bc-HP-Pro-3090-Microtower-PC:~/bheem/Ballot2$ truffle compile Error parsing /home/bc/bheem/Ballot2/contracts/Ballot.sol: ParsedContract.sol:61:1: ParserError: Expected pragma, import directive or contract/interface/library definition. } ^ Compilation failed. See above. Truffle v5.0.2 (core: 5.0.2) Node v8.12.0 – bhemeswararao ankireddy Jan 25 at 6:45
  • 1
    As proposals variable is declared as public . So getCount method is not required, you directly call the method proposals(index). – Smalik Jan 25 at 6:57
1

Mostly you have just warnings, but the first error is: /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. This is the important part and you have to fix this.

Solidity version 0.4.22 introduced new keyword constructor which should be used for constructors. Since version 0.5.0, the use of keyword constructor for a constructor was mandatory - you can no longer use a constructor function which has the same name as the contract name.

So just rename your constructor function from function Ballot() public { to constructor() public {.

Furthermore, you have another error: ,/home/bc/bheem/Ballot2/contracts/Ballot.sol:57:46: TypeError: Data location must be "memory" for return parameter in function, but none was given.

Fix this by adding a memory keyword. Also your returned variable's signature does not match the return signature. You are giving uint[4] memory as parameter and returning it, but the function expects a uint[8] memory as return value. So the function should probably be:

function getCount() public view returns (uint[4] memory) {
    return proposals;
}
  • i have changed my code but still getting error help me ? – bhemeswararao ankireddy Jan 25 at 6:56
  • ,/home/bc/bheem/Ballot2/contracts/Migrations.sol:20:5: TypeError: Name has to refer to a struct, enum or contract. Migrations upgraded = Migrations(new_address); ^--------^ Compilation failed. See above. – bhemeswararao ankireddy Jan 25 at 6:58
  • pragma solidity ^0.5.0; contract Migrations { address public owner; uint public last_completed_migration; modifier restricted() { if (msg.sender == owner) _; } function Migrations() public { owner = msg.sender; } function setCompleted(uint completed) public restricted { last_completed_migration = completed; } function upgrade(address new_address) public restricted { Migrations upgraded = Migrations(new_address); upgraded.setCompleted(last_completed_migration); } } – bhemeswararao ankireddy Jan 25 at 7:00

Your Answer

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

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