Ayan
Ayan

Reputation: 8886

Member "push" not found or not visible after argument-dependent lookup in address payable[] storage ref

In the statement players.push(msg.sender); I am getting following error:

Member "push" not found or not visible after argument-dependent lookup in address payable[] storage ref.

Thus I cannot push to address payable array in solidity. What's the workaround here?

// 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
    }
}

Upvotes: 30

Views: 15548

Answers (2)

Petr Hejda
Petr Hejda

Reputation: 43481

If you're compiling with Solidity 0.7, everything works fine.

This error shows in Solidity 0.8, and it's because in 0.8 msg.sender is not automatically payable anymore. So you need to make it payable first:

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

From the docs page Solidity v0.8.0 Breaking Changes:

The global variables tx.origin and msg.sender have the type address instead of address payable. One can convert them into address payable by using an explicit conversion, i.e., payable(tx.origin) or payable(msg.sender).

Upvotes: 60

Ayan
Ayan

Reputation: 8886

I had to explicitly convert msg.sender into payable to get it working.

// 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);
    } 
}

References:

Casting from address to address payable

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

Upvotes: 8

Related Questions