0

I was making a smart contract in which I wanted to send and receive ether.

My code:

contract coin_transfer_to {
    function() public payable {
    }
    function get_balance() public returns (uint)
    {
        returns address(this).balance;
    }
}

contract coin_transfer_from{
    coin_transfer_to private _instance;
    function coin_transfer_from () public
    {
        _instance = new coin_transfer_to();
    }
    
    function get_balance() public returns (uint)
    {
        return address(this).balance;
    }
    
    function get_balance_of_instance() public returns (uint)
    {
       return _instance.get_balance();
    }
    
    function () public payable 
    {
        address(_instance).send(msg.value);
    }
}

I got the following error in line 2 of above code which is function() public payable:

error in line 2 of above code which is function() public payable{

1
  • It would be helpful if you could edit your question to display the code properly. The contract definition is unreadable. – Undead8 Apr 18 at 0:00
0

In solidity v6 and above payable functions have to be written like this receive() public payable

2
  • it now gives error in the same line as , ParserError : Expected identifier but got '(' recieve() public payable { ^ – user 404 not found Apr 17 at 23:38
  • Oh yes instead of public payable has to be declared external. So write it like receive() external payable – mzaidi Apr 18 at 6:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.