Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solidity, Member "transfer" not found or not visible after argument-dependent

Using Solidity ^0.5.0, I'm actually trying to implement an old code working in ^0.4.17, but it throws an error on Remix. I checked syntax and everything but just can't catch what's wrong. Any help highly appreciated.

pragma solidity ^0.5.0;

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

  constructor() public {
      manager = msg.sender;
  }

   function participate() public payable {
       require(msg.value > .01 ether);
       players.push(msg.sender);
  }

   function pseudoRandom() private view returns(uint){
    return uint(keccak256(abi.encodePacked(block.difficulty, now, players)));
  }

  function pickWinner() public {
    uint index = pseudoRandom() % players.length;
    players[index].transfer(address(this).balance);
  }
}

Here is the error message:

browser/Lottery.sol:22:8: TypeError: Member "transfer" not found or not visible after argument-dependent lookup in address.

players[index].transfer(address(msg.sender).balance);

^---------------------^

like image 242
Torof Avatar asked Nov 27 '18 12:11

Torof


2 Answers

Please see Solidity v0.5.0 Breaking Changes.

As pointed out here:

The address type was split into address and address payable, where only address payable provides the transfer function.

See my answer here for more details.

Change this line:

address[] public players;

To this:

address payable[] public players;

EDIT

Thanks smarx! 😄

like image 71
foba Avatar answered Nov 19 '22 11:11

foba


You are missing the payable modifier on the initial declaration. Change

address[] public players;

to

address payable[] public players;

like image 41
Jose Pablo Avatar answered Nov 19 '22 10:11

Jose Pablo