Type address is not implicitly convertible to expected type address payable

Viewed 494

I am taking CryptoZombies Solidity tutorial and I have got such a problem:

Type address is not implicitly convertible to expected type address payable.

Here is the code:

function withdraw() external onlyOwner {
        address payable _owner = address(uint160(owner()));
        _owner.transfer(address(this).balance);
    }

I use VSCode and a Solidity extension. The IDE highlights this line in red:

address payable _owner = address(uint160(owner()));

and displays the above error. What should I do to avoid this error?

1 Answers

You can use the payable() typecast function to change the type of an address to address payable.

// assuming `owner()` returns an `address`
// it's redundant to convert it to `uint160` and then back to `address`
address payable _owner = payable(owner());
Related