【问题标题】:Member "push" not found or not visible after argument-dependent lookup in address payable[] storage ref在地址支付 [] 存储参考中进行参数相关查找后,成员“推送”未找到或不可见
【发布时间】:2021-06-22 06:07:27
【问题描述】:

在声明 players.push(msg.sender); 我收到以下错误:

在地址payable[]存储参考中进行参数相关查找后,成员“推送”未找到或不可见。

因此,我无法稳定地解决应付数组。这里有什么解决方法?

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0;

contract Lottery {
    address public manager;
    address payable[] public players;

    constructor() {
        manager = msg.sender;
    }

    function enter() public payable {
        players.push(msg.sender);            // ERROR IN THIS LINE
    }
}

【问题讨论】:

    标签: ethereum solidity


    【解决方案1】:

    如果您使用 Solidity 0.7 进行编译,一切正常。

    此错误显示在 Solidity 0.8 中,这是因为在 0.8 中 msg.sender 不再自动为 payable。所以你需要先把它设为payable

    players.push(payable(msg.sender));
    

    【讨论】:

      【解决方案2】:

      我必须将 msg.sender 显式转换为 payable 才能使其正常工作。

      // SPDX-License-Identifier: GPL-3.0
      
      pragma solidity >=0.7.0;
      
      contract Lottery {
          address payable public manager;
          address payable[] public players;
          
          constructor() {
              manager = payable(msg.sender);
          }
          
          function enter() public payable {
              players.push(manager);
          } 
      }
      

      参考资料:

      Casting from address to address payable

      TypeError: push is not detected as a function for address payable dynamic array

      【讨论】:

        猜你喜欢
        • 2021-06-11
        • 2021-10-21
        • 1970-01-01
        • 2019-04-29
        • 2021-12-29
        • 2016-06-09
        • 2021-02-18
        相关资源
        最近更新 更多