[Ethereum] Function, variable, struct or modifier declaration expected

compilergo-ethereumtruffle

I am using the following code structure;

pragma solidity ^0.5.0;

  contract TtdmToken {

    uint256 public totalSupply;

    function TtdmToken () public {
        totalSupply = 1000000;
    }
   }

But when compiling with truffle compile I am getting the following error,

enter image description here

I have seen similar issue here suggesting the change of contract name I changed the contract to the following but still getting similar results.

pragma solidity ^0.5.0;

  contract TtdmToken {

    uint256 public totalSupply;

    function TtdmToken () public {
        totalSupply = 1000000;
    }

    contract constructor_TtdmToken {
        function TtdmToken()

    }

   }

And this is the error after making the change;

enter image description here

Best Answer

The compiler complains because function TtdmToken() ends unexpectedly (your second attempt).

Use constructor() instead of function() (of any name) to make a constructor.

pragma solidity ^0.5.0;

contract TtdmToken {

  uint256 public totalSupply;

  constructor () public {
    totalSupply = 1000000;
  }
}

Hope it helps.

Related Topic