1

Can someone explain to me about this error? I just tried override feature in Solidity v0.6, but I got error Expected '{' but got reserved keyword 'override' function totalSupply() public override view returns (uint256) when compile contract.

pragma solidity >=0.4.21 <0.7.0;

import "./IERC20.sol";

    contract ERC20 is IERC20 {

    mapping (address => uint256) private _balances;

    mapping (address => mapping (address => uint256)) private _allowances;

    uint256 private _totalSupply;

    /**
    * @dev See `IERC20.totalSupply`.
    */
    function totalSupply()  public override  view returns (uint256) {
        return _totalSupply;
    } 
} 

Interface contract

pragma solidity >=0.4.21 <0.7.0;

interface IERC20 {
        /**
         * @dev Returns the amount of tokens in existence.
         */
        function totalSupply() external view returns (uint256);
}
  • I would guess that you need to declare function totalSupply in interface IERC20 as virtual. – goodvibration Apr 22 at 10:24
  • Also, note that your overriding function should probably be external instead of public. – goodvibration Apr 22 at 10:25
  • thanks @goodvibration, i have tried both solution, but still return same error. – Al-kadafi Apr 22 at 10:34
1

I think you missed few '}' at the end of your contract and interface. the following works perfectly :

pragma solidity >=0.4.21 <0.7.0;

interface IERC20 {
    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);
}

contract ERC20 is IERC20 {

mapping (address => uint256) private _balances;

mapping (address => mapping (address => uint256)) private _allowances;

uint256 private _totalSupply;

/**
* @dev See `IERC20.totalSupply`.
*/
function totalSupply()  public override  view returns (uint256) {
    return _totalSupply;
}  
}
| improve this answer | |
  • Sorry, I intentionally didn't attach all the codes. But now I try to only use 1 function on the interface and 1 override function like your solution and still return same error. – Al-kadafi Apr 23 at 3:19
0

I had the same problem and I solved it setting the Solidity compiler version to 0.6.8 (the latest at the current moment).

This can be done on Remix in the Compiler section, or in Truffle editing the file truffle-config.js.

| improve this answer | |

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.