类型地址不能隐式转换为预期类型地址 payable
Type address is not implicitly convertible to expected type address payable
我正在学习 CryptoZombies Solidity 教程,我遇到了这样的问题:
Type address is not implicitly convertible to expected type address payable.
代码如下:
function withdraw() external onlyOwner {
address payable _owner = address(uint160(owner()));
_owner.transfer(address(this).balance);
}
我使用 VSCode 和 Solidity 扩展。 IDE 以红色突出显示此行:
address payable _owner = address(uint160(owner()));
并显示上述错误。我应该怎么做才能避免这个错误?
您可以使用 payable()
类型转换函数将 address
的类型更改为 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());
我正在学习 CryptoZombies Solidity 教程,我遇到了这样的问题:
Type address is not implicitly convertible to expected type address payable.
代码如下:
function withdraw() external onlyOwner {
address payable _owner = address(uint160(owner()));
_owner.transfer(address(this).balance);
}
我使用 VSCode 和 Solidity 扩展。 IDE 以红色突出显示此行:
address payable _owner = address(uint160(owner()));
并显示上述错误。我应该怎么做才能避免这个错误?
您可以使用 payable()
类型转换函数将 address
的类型更改为 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());