0

I am trying the function override capability in solidity 0.6.0. However, when I am trying to override a function in the same contract it is giving me an error that the function has been defined twice. Here is the code:

contract derivedContractv2 {

    function add(uint newa, uint newb) virtual public pure returns (uint _newValue) {
        return newa+newb;
    }

    function add(uint newesta, uint newestb) override public pure returns (uint _newestValue) {
        return newesta+newestb;
    }

}

The error generated is as follows:

browser/integratedContract.sol:13:5: DeclarationError: Function with same name and arguments defined twice.
function add(uint newa, uint newb) virtual public pure returns (uint _newValue) {
^ (Relevant source part starts here and spans across multiple lines).
browser/integratedContract.sol:17:5: Other declaration is here:
function add(uint newesta, uint newestb) override public pure returns (uint _newestValue) {
^ (Relevant source part starts here and spans across multiple lines).

and

browser/integratedContract.sol:17:46: TypeError: Function has override specified but does not override anything.
function add(uint newesta, uint newestb) override public pure returns (uint _newestValue) {
^------^

Is there a way in which I can override the function defined within the same contract?

2

The virtual function must be in a parent contract. The override function must be in a child contract. Now you have both the functions in the same contract.

1
  • oh ok. While that worked. I was literally going for word to word over here: ``` Functions can now only be overridden when they are either marked with the virtual keyword or defined in an interface. Functions without implementation outside an interface have to be marked virtual. When overriding a function or modifier, the new keyword override must be used. ``` Which I thought also can work for the same contract. Thank you anyway @mikko – AmateurDev Apr 16 '20 at 10:19

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.