3

警告:不推荐使用从地址类型继承的合约成员“余额”。将合约转换为“address”类型以访问成员,例如使用“address(contract).balance”代替。

我使用 Remix 编辑器在 Solidity 中收到此警告。

这是代码块:

function getSummary() public view returns(
    uint, uint, uint, uint, address
){
    return (
        minimumContribution,
        this.balance, // This is the warning line.
        requests.length,
        approversCount,
        manager
    );
}

我尝试遵循警告的建议:

function getSummary() public view returns(
    uint, uint, uint, uint, address
){
    return (
        minimumContribution,
        address(contract).balance,
        requests.length,
        approversCount,
        manager
    );
}

但这不起作用。

4

2 回答 2

5

balanceaddress类型的属性,而不是来自合同。将其更改为address(this).balance.

function getSummary() public view returns(
    uint, uint, uint, uint, address
){
    return (
        minimumContribution,
        address(this).balance,
        requests.length,
        approversCount,
        manager
    );
}
于 2018-07-17T02:35:16.683 回答
2

或者,您可以分配给类型为...this的局部变量address

address contractAddress = this;

function getSummary() public view returns(
    uint, uint, uint, uint, address
){
  return (
    minimumContribution,
    contractAddress.balance,
    requests.length,
    approversCount,
    manager
  );
}
于 2018-07-18T07:38:17.770 回答